使用poi获取excel中的单元格颜色

时间:2021-08-08 20:19:54

I read all questions on so but no answer was working. If i change the cell color in excel i still get 0 and 64. I am using excel 2007 and poi 3.11. Thanks for help.

我阅读了所有的问题,但没有答案。如果我改变excel中的单元格颜色,仍然得到0和64。我正在使用excel 2007和poi 3.11。谢谢你的帮助。

try{ File file = new File("C:\\Test\\poi.xlsx");
    FileInputStream fis = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sh = wb.getSheet("Tabelle1");
    XSSFCellStyle cs = sh.getRow(0).getCell(0).getCellStyle();   
    System.out.println("Color: "+cs.getFillForegroundColor()); // 0
    System.out.println("Color: "+cs.getFillBackgroundColor()); // 64
    }catch(Exception e){e.printStackTrace();}

1 个解决方案

#1


1  

cs.getFillForegroundColorColor().getARGBHex()

This will return the ARGB in hex

这将返回十六进制的ARGB

UPDATE
To check the color, you can do:

更新查看颜色,你可以做:

Color color = cs.getFillForegroundColorColor();

if (color.GREY_25_PERCENT || color.GREY_40_PERCENT || color.GREY_50_PERCENT || color.GREY_80_PERCENT)
    // is grey
}

OR if it's only 2 colors used:

或者如果只使用两种颜色:

Color color = cs.getFillForegroundColorColor();

if (color.WHITE) {
  // is white
}else {
  // is grey
}

#1


1  

cs.getFillForegroundColorColor().getARGBHex()

This will return the ARGB in hex

这将返回十六进制的ARGB

UPDATE
To check the color, you can do:

更新查看颜色,你可以做:

Color color = cs.getFillForegroundColorColor();

if (color.GREY_25_PERCENT || color.GREY_40_PERCENT || color.GREY_50_PERCENT || color.GREY_80_PERCENT)
    // is grey
}

OR if it's only 2 colors used:

或者如果只使用两种颜色:

Color color = cs.getFillForegroundColorColor();

if (color.WHITE) {
  // is white
}else {
  // is grey
}