Get Cell color in excel with poi

2019-08-15 07:10发布

问题:

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.

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:

cs.getFillForegroundColorColor().getARGBHex()

This will return the ARGB in hex

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
}