简体   繁体   English

访问XSSFWorkbook中的调色板

[英]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. 使用POI时,Excel文档中的单元格和字体包含的颜色信息并不总是返回rgb值,而且通常只提供索引值。 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: 在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. 访问XSSFWorkbook(xlsx)时,没有这样的方法,实际上我在相关类中的任何地方都找不到调色板信息。 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. 我能够从XSSFont和Cell获得索引值,但获得颜色“名称”的唯一方法是将其与IndexedColors枚举相匹配。 This returns me to the same original problem; 这让我回到了同样的原始问题; I still have no rgb value to use. 我仍然没有使用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. 更新1:我发现最终有效的东西。 This method of XSSFColor returns the ARGB hex code and with it I can determine the RGB values (obviously). 这种XSSFColor方法返回ARGB十六进制代码,用它我可以确定RGB值(显然)。 I hope this helps save x number of hours for someone with the same issue. 我希望这有助于为有相同问题的人节省x个小时。

((XSSFColor) color).getARGBHex())

Update 2: Much to my dismay, I've found that some Cells don't return background XSSFColor containing ARGBHex data. 更新2:令我沮丧的是,我发现一些Cell不会返回包含ARGBHex数据的背景XSSFColor。 Looking for a work-around for this. 寻找解决方案。

Using wb.getStylesSource() , you can get a StylesTable , from which you can get all the CellStyle objects. 使用wb.getStylesSource() ,您可以获得一个StylesTable ,您可以从中获取所有CellStyle对象。 The XSSFCellStyle API has any number of methods to get color objects - namely, an XSSFColor . XSSFCellStyle API有许多方法来获取颜色对象 - 即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. XSSFCellStyle API还可以访问该样式中的所有字体 - 即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值的字节数组。

You are not going to get exactly what you are looking for here. 你不会得到你正在寻找的东西。 There isn't an equivalent XSSF version of HSSFPalette. HSSFPalette没有等效的XSSF版本。 There isn't a need for it since the HSSFWorkbook had a very limited amount of colors it could work with. 因为HSSFWorkbook的颜色数量非​​常有限,所以不需要它。 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. 如果你只是问我如何弄清楚当我有一个XSSFColor对象时getRGB()的返回意味着什么颜色,你总是可以参考这个网站,它可以让你输入RGB值并看到颜色,如果你正在寻找一个颜色名称,你必须基本上创建自己的实用程序,它将存储已知的颜色rgb值,并有一些方法看看哪个最接近你返回的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. 这是我能做的最好的人,我不知道能够为你提供开箱即用功能的东西。

One thing to look out for is that Excel reverses the meaning of foreground and background for the solid fill pattern in ordinary cells*. 需要注意的一点是,Excel反转普通单元格中实体填充图案的前景和背景的含义*。 So maybe you need to use the getFillForegroundColorColor() method for cells with a solid pattern type. 因此,您可能需要对具有实体模式类型的单元格使用getFillForegroundColorColor()方法。

Also, reading back to your previous question, 64 isn't a valid colour index since the range is 0..63. 另外,回读上一个问题,64不是有效的颜色索引,因为范围是0..63。 The index 64 is used to indicate the default foreground colour in a cell. 索引64用于指示单元格中的默认前景色。

(*) In conditional format cells it doesn't do this!! (*)在条件格式单元格中它不会这样做!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM