Java 访问 XSSFWorkbook 中的调色板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18112155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:28:38  来源:igfitidea点击:

Access to the color palette in an XSSFWorkbook

javacolorsapache-poi

提问by Paul Gregtheitroade

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:

使用 POI 时,excel 文档中的单元格和字体包含颜色信息,这些信息并不总是返回 rgb 值,而且通常只提供索引值。索引值必须根据某些东西查找才能获得颜色。在 HSSFWorkbook (xls) 中有一种方法可用于获取调色板:

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.

访问 XSSFWorkbook (xlsx) 时没有这样的方法,事实上我在相关类的任何地方都找不到调色板信息。我能够从 XSSFont 和 Cell 中获取索引值,但是获取颜色“名称”的唯一方法是将它与 IndexedColors 枚举进行匹配。这让我回到了同样的原始问题;我仍然没有 rgb 值可以使用。

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 获取 XSSFColor,如下所示:

CellStyle style = cell.getCellStyle();
XSSFColor color = style.getFillBackgroundColorColor();

To get a color name via IndexedColors:

通过 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?

类似问题:如何获取给定单元格的(Java Apache POI HSSF)背景颜色?

Reference: http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors

参考: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.

更新 1:我终于找到了一些有用的东西。XSSFColor 的这种方法返回 ARGB 十六进制代码,我可以用它来确定 RGB 值(显然)。我希望这有助于为遇到相同问题的人节省 x 小时。

((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.

更新 2:令我沮丧的是,我发现某些单元格不返回包含 ARGBHex 数据的背景 XSSFColor。正在为此寻找解决方法。

采纳答案by akokskis

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

使用wb.getStylesSource(),您可以获得一个StylesTable,您可以从中获取所有CellStyle对象。该XSSFCellStyleAPI有任何数量的方法来获得颜色对象-即,一XSSFColor。该XSSFCellStyleAPI还可以访问该样式中的所有字体-即XSSFFont,从中可以再次获得XSSFColor该特定字体对象。

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

一旦您获得了访问权限XSSFColor,调用getRGB()将返回一个 RGB 值的字节数组。

回答by Ithamar Diaz

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 linkyou 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 thiswebsite 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.

你不会在这里得到你正在寻找的东西。HSSFPalette 没有等效的 XSSF 版本。不需要它,因为 HSSFWorkbook 可以使用的颜色数量非​​常有限。您提供的链接中给出的说明是您将获得的最接近的说明。如果您只是问我如何弄清楚一旦我有一个 XSSFColor 对象, getRGB() 的返回意味着什么颜色,您总是可以参考这个网站,它可以让您输入 RGB 值并查看颜色,如果您正在寻找颜色名称,您必须基本上创建自己的实用程序,该实用程序将存储已知的颜色 rgb 值,并使用某种方法查看哪个最接近您的返回 RGB。这是我能做的最好的人,我不知道有什么东西可以为您提供开箱即用的功能。

回答by jmcnamara

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.

需要注意的一件事是,Excel 颠倒了普通单元格中实心填充图案的前景和背景的含义*。因此,也许您需要对getFillForegroundColorColor()具有实体图案类型的单元格使用该方法。

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.

此外,回顾您之前的问题,64 不是有效的颜色索引,因为范围是 0..63。索引 64 用于指示单元格中的默认前景色。

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

(*) 在条件格式单元格中它不会这样做!!