访问 XSSFWorkbook 中的调色板 [英] Access to the color palette in an XSSFWorkbook

查看:74
本文介绍了访问 XSSFWorkbook 中的调色板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

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

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!

我通过 CellStyle 获取 XSSFColor,如下所示:

I am getting the XSSFColor by way of the CellStyle, like so:

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

通过 IndexedColors 获取颜色名称:

To get a color name via IndexedColors:

for (IndexedColors c : IndexedColors.values()) { if (c.index == indexColor){ System.out.println("Color: " + c.name()); } }

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

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

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

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

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

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.

推荐答案

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

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.

一旦您访问了该 XSSFColor,调用 getRGB() 将返回一个 RGB 值的字节数组.

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

这篇关于访问 XSSFWorkbook 中的调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆