Java 如何使用apache poi获取单元格的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19491231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get cell's background color using apache poi?
提问by AAA
How do we get background color
of a XSSFCell
. I tried using XSSFCellStyle
but no luck.
我们如何获得background color
一个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.
使用这些步骤,我无法获得Short
类型的背景颜色表示。
回答by YoBre
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
回答by Kartik Narayana Maringanti
Checkout this 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 自定义颜色选择器中的实际颜色代码相比,它或多或少返回了相同的颜色。
回答by Ascalonian
Try this:
尝试这个:
row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()
Notice that Color
is used twice
注意Color
使用了两次
回答by Anthony Holland
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 中,但它确实显示了如何从对象模型中获取颜色。我想从实际的 rgb 值实例化一个 java.awt.Color 对象(这很有用,部分是因为我的调试器在我停在断点处时为我显示了对象的实际颜色,部分是因为这是为了导出到具有与 Excel 无关)。我忽略了颜色的 alpha 值,我的 Scala 可能有点幼稚。我建议,如果这对您不起作用,您应该设置一个断点并检查密切相关的方法调用的结果,例如 getFillBackgroundColorColor()
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))
回答by shivaji
All the time its giving me no 64 this is my code
一直没有给我 64 这是我的代码
for(Row r : my_sheet) {
for (Cell c : r) {
System.out.println(c.getCellStyle().getFillBackgroundColor() );
//if foreground filter color is not green then hide the record
if ( c.getColumnIndex()==1 && c.getCellStyle().getFillBackgroundColor() !=17){
r1=(XSSFRow) c.getRow();
if (r1.getRowNum()!=0) { /* Ignore top row */
/* Hide Row that does not meet Filter Criteria */
r1.getCTRow().setHidden(true); }
}
}
}