Access to the color palette in an XSSFWorkbook

2019-02-12 02:17发布

问题:

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.

回答1:

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.



回答2:

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.



回答3:

One thing to look out for is that Excel reverses the meaning of foreground and background for the solid fill pattern in ordinary cells*. So maybe you need to use the getFillForegroundColorColor() method for cells with a solid pattern type.

Also, reading back to your previous question, 64 isn't a valid colour index since the range is 0..63. The index 64 is used to indicate the default foreground colour in a cell.

(*) In conditional format cells it doesn't do this!!