如何使用Apache POI获取单元格的背景颜色?

10

如何获取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());

使用这些步骤,我无法在Short类型中获得背景颜色表示。

请发布你已经尝试过的代码。 - Woody
1
@AAA:你的代码看起来是正确的...在这种情况下你得到了什么结果...默认情况下它在我的端上显示为64... - Sankumarsingh
@Sankumarsingh 我也得到了64,这是自动颜色代码,而你的工作簿却有不同的颜色,这没有任何意义。 - AAA
但是尝试这个: cell.getCellStyle().setFillForegroundColor(HSSFColor.GOLD.index); System.out.println(cell.getCellStyle().getFillForegroundColor());它会给你51...这意味着它正在工作并根据所得到的颜色进行更改。 - Sankumarsingh
@Sankumarsingh,我已经尝试过这个方法了,能够获取到64以外的其他值,但是如果我在Excel中使用任何工具而不是POI进行设置,我无法获取背景颜色。我正在使用的Excel文件不是通过POI生成的,而是使用Microsoft Excel等工具生成的。 - AAA
你找到答案了吗? - Larsen
7个回答

3

查看此网址:

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;
}

这将返回RGB代码,但并非精确的代码。但与XLS自定义颜色选择器中的实际颜色代码相比,它更或多或少返回相同的颜色。

调色板是指什么? - Larsen
当cellStyle.getFillForegroundColor()或cellStyle.getFillBackgroundColor()始终返回默认值时,这将如何工作? - Larsen

2

试试这个:

row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()

请注意,Color被使用了两次。

0
以下是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))

0

一直都在给我返回无 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); }
            }
        }
    }

我也是一样的。 - Larsen

0

获取背景颜色似乎有多种选项。当单元格的颜色是以以下方式指定时:

Background color selection in excel

以下返回颜色代码:

public static String getBackgroundColor(XSSFCell cell) {
  byte[] rgbWithTint = cell.getCellStyle().getFillForegroundColorColor().getRGBWithTint();
  if (rgbWithTint == null) {
    return null;
  }
  return Hex.encodeHexString(rgbWithTint);
}

0
我也遇到了一个问题,每次尝试从单元格中读取颜色时,只能得到颜色索引64
经过多次尝试,我发现工作表启用了条件着色。如果是这种情况,似乎无法通过cell.getCellStyle().getFillForegroundColorColor()来读取背景颜色。
在意识到这一点之后,在我的使用案例中,我只需查看颜色所基于的条件,并提取所需的信息。因此,我不需要读取实际的颜色,只需要区分状态。
无论如何,我相信使用Apache POI有可能读取这种有条件填充的颜色。一个很好的起点可能是 Quick-Guide to conditionally coloring

0

我正在使用Scala,但是它和你的代码一样正确。

这是我的代码,请看看是否有区别:

val wb = new XSSFWorkbook(path)
for (id <- 0.until(sheetTot)) {
    val sh = wb.getSheetAt(id)    
    print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor())
}

在我的情况下,结果是64。

如果您每次检查不同的背景颜色,它将为您提供值64,这是“自动”颜色。 - AAA
1
是的,64是自动的意思。但我们如何获得实际的颜色呢? - Larsen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接