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

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

问题描述

在Excel文档中的使用POI,细胞和字体包含的颜色信息,这并不总是返回一个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和细胞,但只有这样的索引值得到这么多颜色name是与之相匹配的对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()); } }

类似的问题:<一href=\"http://stackoverflow.com/questions/1499739/how-do-i-get-the-java-apache-poi-hssf-background-color-for-a-given-cell?rq=1\">How做我得到一个给定的单元格(Java的Apache的POI HSSF)背景颜色?

参考: http://poi.apache.org/ US preadsheet /快guide.html#CustomColors

更新1:我已经找到了一些作品,终于。 XSSFColor此方法返回的ARGB十六进制code和与它我能确定的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(),你可以得到一个<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/xssf/model/StylesTable.html\"><$c$c>StylesTable,从中你可以得到所有的 CellStyle 的对象。在<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFCellStyle.html\"><$c$c>XSSFCellStyle API有任何数量的方法来获得颜色对象 - 即,一<一href=\"http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFColor.html\"><$c$c>XSSFColor.在 XSSFCellStyle API还可以访问该样式中的所有字体 - 即,<一个href=\"http://poi.apache.org/apidocs/org/apache/poi/xssf/usermodel/XSSFFont.html\"><$c$c>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天全站免登陆