How do we get background color
of a XSSFCell
. I tried using XSSFCellStyle
but no luck.
如何获得XSSFCell的背景色。我尝试过使用XSSFCellStyle,但没有成功。
FileInputStream fis = new FileInputStream(fileName);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(0);
XSSFRow row = sheet.getRow(0);
System.out.println(row.getCell(0).getCellStyle().getFillForegroundColor());
Using these steps I am not able to get background color representation in Short
type.
使用这些步骤,我无法获得短类型的背景颜色表示。
4 个解决方案
#1
2
Checkout this URL:
检查这个URL:
https://issues.apache.org/bugzilla/show_bug.cgi?id=45492
https://issues.apache.org/bugzilla/show_bug.cgi?id=45492
Cell cell = row.getCell(1);
CellStyle cellStyle = cell.getCellStyle();
System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor()));
private short[] getColorPattern(short colorIdx){
short[] triplet = null;
HSSFColor color = palette.getColor(colorIdx);
triplet = color.getTriplet();
System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
return triplet;
}
This returns the RGB codes but not exact ones. But its more or less the same color returned when compared with the actual color code in the XLS custom color picker.
这将返回RGB代码,但不返回精确的代码。但是与XLS自定义颜色选择器中的实际颜色代码相比,它的颜色或多或少都是相同的。
#2
1
Try this:
试试这个:
row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()
Notice that Color
is used twice
注意,颜色使用了两次。
#3
0
I'm working in scala but it's the same. Your code is right.
我在scala工作,但都是一样的。你的代码是正确的。
This is my, see if you can find differences:
这是我的,看看你能不能找到不同之处:
val wb = new XSSFWorkbook(path)
for (id <- 0.until(sheetTot)) {
val sh = wb.getSheetAt(id)
print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor())
}
in my case the result is 64
我的结果是64
#4
0
The following is in Scala but it does show exactly how to get the colour from the object model. I wanted to instantiate a java.awt.Color object from the actual rgb values (which is useful partly because my debugger displays for me the actual colour of the object when I stop at breakpoints, and partly because this is for export to systems that have nothing to do with Excel). I'm ignoring the colour's alpha value and my Scala may be a bit naive. I'd suggest that if this doesn't work for you, you should set a break-point and examine the result of closely related method calls such as getFillBackgroundColorColor()
下面是Scala的代码,但是它确实展示了如何从对象模型中获得颜色。我想实例化一个java.awt。使用rgb值的实际颜色对象(这很有用,部分原因是我的调试器在断点处停止时为我显示对象的实际颜色,部分原因是这是用于导出到与Excel无关的系统)。我忽略了颜色的alpha值,我的Scala可能有点幼稚。我建议,如果这对您不起作用,您应该设置断点并检查与之密切相关的方法调用的结果,如getfillbackgroundcolor()
val rgb: Array[Byte] = cell.getCellStyle.getFillForegroundColorColor.getRgb
def toInt(b: Byte): Int = {
if (b<0) 256+b else b
}
val rgbInts = rgb.map(toInt)
val color = new Color(rgbInts(0),rgbInts(1),rgbInts(2))
#1
2
Checkout this URL:
检查这个URL:
https://issues.apache.org/bugzilla/show_bug.cgi?id=45492
https://issues.apache.org/bugzilla/show_bug.cgi?id=45492
Cell cell = row.getCell(1);
CellStyle cellStyle = cell.getCellStyle();
System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor()));
private short[] getColorPattern(short colorIdx){
short[] triplet = null;
HSSFColor color = palette.getColor(colorIdx);
triplet = color.getTriplet();
System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
return triplet;
}
This returns the RGB codes but not exact ones. But its more or less the same color returned when compared with the actual color code in the XLS custom color picker.
这将返回RGB代码,但不返回精确的代码。但是与XLS自定义颜色选择器中的实际颜色代码相比,它的颜色或多或少都是相同的。
#2
1
Try this:
试试这个:
row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()
Notice that Color
is used twice
注意,颜色使用了两次。
#3
0
I'm working in scala but it's the same. Your code is right.
我在scala工作,但都是一样的。你的代码是正确的。
This is my, see if you can find differences:
这是我的,看看你能不能找到不同之处:
val wb = new XSSFWorkbook(path)
for (id <- 0.until(sheetTot)) {
val sh = wb.getSheetAt(id)
print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor())
}
in my case the result is 64
我的结果是64
#4
0
The following is in Scala but it does show exactly how to get the colour from the object model. I wanted to instantiate a java.awt.Color object from the actual rgb values (which is useful partly because my debugger displays for me the actual colour of the object when I stop at breakpoints, and partly because this is for export to systems that have nothing to do with Excel). I'm ignoring the colour's alpha value and my Scala may be a bit naive. I'd suggest that if this doesn't work for you, you should set a break-point and examine the result of closely related method calls such as getFillBackgroundColorColor()
下面是Scala的代码,但是它确实展示了如何从对象模型中获得颜色。我想实例化一个java.awt。使用rgb值的实际颜色对象(这很有用,部分原因是我的调试器在断点处停止时为我显示对象的实际颜色,部分原因是这是用于导出到与Excel无关的系统)。我忽略了颜色的alpha值,我的Scala可能有点幼稚。我建议,如果这对您不起作用,您应该设置断点并检查与之密切相关的方法调用的结果,如getfillbackgroundcolor()
val rgb: Array[Byte] = cell.getCellStyle.getFillForegroundColorColor.getRgb
def toInt(b: Byte): Int = {
if (b<0) 256+b else b
}
val rgbInts = rgb.map(toInt)
val color = new Color(rgbInts(0),rgbInts(1),rgbInts(2))