Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to the color palette in an XSSFWorkbook

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.

like image 999
Paul Gregoire Avatar asked Aug 07 '13 19:08

Paul Gregoire


People also ask

How do I use XSSFWorkbook?

XSSFWorkbook(java.io.InputStream is) Constructs an XSSFWorkbook object, by buffering the whole input stream into memory and then opening an OPCPackage object for it.


2 Answers

Using wb.getStylesSource(), you can get a StylesTable, from which you can get all the CellStyle objects. The XSSFCellStyle API has any number of methods to get color objects - namely, an XSSFColor. The XSSFCellStyle API also has access to all the fonts within that style - namely, XSSFFont, from which you can again get a XSSFColor object for that specific font.

Once you've gotten access to that XSSFColor, a call to getRGB() will return you a byte array of the RGB values.

like image 145
akokskis Avatar answered Sep 21 '22 05:09

akokskis


You are not going to get exactly what you are looking for here. There isn't an equivalent XSSF version of HSSFPalette. There isn't a need for it since the HSSFWorkbook had a very limited amount of colors it could work with. The instructions given in the link you provided is the closest you will get. If you are simply asking how do I figure out what color is meant by the return of getRGB() once I have an XSSFColor object, you could always refer to this website which will let you enter the RGB values and see the color, if you are looking for a color name you will have to basically create your own utility that will have stored known rgb values for colors and have some method see which is closest to your returned RGB. That's the best I can do man, I am not aware of something that will give you this functionality out of the box.

like image 43
Ithamar Diaz Avatar answered Sep 19 '22 05:09

Ithamar Diaz