When using POI, cells and fonts in excel documents contain color information which does not always return an rgb value and it often only offers an index value. The indexed value must be looked up against something to get a color. In an HSSFWorkbook (xls) there is a method to available to get the palette:
InputStream in = new FileInputStream("sheet.xls");
HSSFWorkbook wb = new HSSFWorkbook(in);
wb.getCustomPalette();
When accessing an XSSFWorkbook (xlsx) there is no such method and in fact I can find no palette information anywhere in the related classes. I am able to get the index value from XSSFont and Cell, but the only way to get so much as a color "name" is to match it against the IndexedColors enum. This returns me to the same original problem; I still have no rgb value to use.
InputStream in = new FileInputStream("sheet.xlsx");
XSSFWorkbook wb = new XSSFWorkbook (in);
wb.getCustomPalette(); <-- fail!
I am getting the XSSFColor by way of the CellStyle, like so:
CellStyle style = cell.getCellStyle();
XSSFColor color = style.getFillBackgroundColorColor();
To get a color name via IndexedColors:
for (IndexedColors c : IndexedColors.values()) { if (c.index == indexColor){ System.out.println("Color: " + c.name()); } }
Similar questions: How do I get the (Java Apache POI HSSF) Background Color for a given cell?
Reference: http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors
Update 1: I've found something that works, finally. This method of XSSFColor returns the ARGB hex code and with it I can determine the RGB values (obviously). I hope this helps save x number of hours for someone with the same issue.
((XSSFColor) color).getARGBHex())
Update 2: Much to my dismay, I've found that some Cells don't return background XSSFColor containing ARGBHex data. Looking for a work-around for this.