如何获取给定单元格的 (Java Apache POI HSSF) 背景颜色?
我有一个现有的 Excel 电子表格,我正在使用 Apache POI HSSF 访问该电子表格并从中读取值。
它的初始化如下:
HSSFSheet sheet;
FileInputStream fis = new FileInputStream(this.file);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);
this.sheet = wb.getSheet(exsheet);
我正在迭代工作表中存在的所有单元格,这会生成一个单元格对象:
HSSFCell cell = (HSSFCell) cells.next();
请熟悉该框架的人解释如何创建一个 (HSSFColor) 对象来表示每个单元格的背景颜色床单。
非常感谢
编辑,更新
要清楚我想知道的是:如何为现有单元格的背景颜色创建/获取HSSFColor对象?
cell.getCellStyle().getFillBackgroundColor();
此代码仅返回一个短数字,而不是 HSSFColor 对象。 感谢到目前为止的回答。
I have an existing excel spreadsheet, which I am accesssing and reading values from, I am using Apache POI HSSF.
It is initialised like this:
HSSFSheet sheet;
FileInputStream fis = new FileInputStream(this.file);
POIFSFileSystem fs = new POIFSFileSystem(fis);
HSSFWorkbook wb = new HSSFWorkbook(fs);
this.sheet = wb.getSheet(exsheet);
I am iterating over all the cells that exist in the sheet, which makes a cell object:
HSSFCell cell = (HSSFCell) cells.next();
Please can someone familiar with the framework explain how to create an (HSSFColor) object to represent the backround color of each cell in the sheet.
Many thanks
EDIT, UPDATE
To be clear what I want to know is: how do I create/get an HSSFColor object for the background color of an existing cell?
cell.getCellStyle().getFillBackgroundColor();
This code only returns a short number, not an HSSFColor object.
Thanks for the answers so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
HSSFCell 类提供了静态颜色类,如下所示:
http://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html
如果您想创建自己的自定义颜色,则需要创建和修改自定义调色板。 Apache 对此也提供了非常清晰的指南:
http://poi.apache。 org/spreadsheet/quick-guide.html#CustomColors
There are static color classes provided by the HSSFCell class, listed here:
http://poi.apache.org/apidocs/org/apache/poi/hssf/util/HSSFColor.html
If you want to create your own custom colors, you will need to create and modify a custom palette. Apache provides a very clear guide to this as well:
http://poi.apache.org/spreadsheet/quick-guide.html#CustomColors
获取颜色:
getFillBackgroundColor 返回的短值是颜色的 Excel 索引。
您可以使用 RMorrisey 指示的最后一个代码来获取与 HSSFColor HashTable 中的索引相对应的颜色。
设置颜色:
您创建一个自定义调色板,并更改给定索引处的颜色。然后,将颜色应用于样式。
问候
纪尧姆
To get the color :
The short value returned by the getFillBackgroundColor is the Excel index of the color.
You can get the color corresponding to the index in the HSSFColor HashTable, using the last code RMorrisey indicated.
To set a color :
You create a custom palette, and change the color at a given index. Then, you apply the color to the style.
Regards
Guillaume
XSSFCellStyle的backgroundcolor信息可以从该方法中获取:
你可以打印出来,你会看到它的结构。
The backgroundcolor information of XSSFCellStyle can get from the method:
You can print it out and you will see it's structure.
您会这样做:
我相信给定的工作簿的样式数量有限;您将希望尽可能重用相同的样式对象。
[编辑:抱歉,这将是设置单元格的颜色。要获取颜色,请使用类似:
]
[编辑2:查看Craig发布的自定义颜色信息,也许你可以尝试:
]
You would do something like:
I believe there is a limited number of styles for a given workbook; you will want to reuse the same style object where possible.
[Edit: Sorry, that would be to set the color on a cell. To get the color, use like:
]
[Edit 2: Looking at the custom color information craig posted, maybe you can try:
]
要获取十六进制中特定单元格的背景颜色,请使用以下命令:
注意单词
Color
使用了两次To get the background color of the specific cell in HEX, use this:
Notice the word
Color
is used twice以下内容适用于 XSSF,使用 Scala,但它确实准确地展示了如何从对象模型获取颜色。我想从实际的 rgb 值实例化一个 java.awt.Color 对象(这很有用,部分原因是我的调试器在我停在断点处时为我显示对象的实际颜色,部分原因是这是为了导出到具有以下功能的系统:与 Excel 无关)。我忽略了颜色的 alpha 值,我的 Scala 可能有点幼稚。我建议,如果这对您不起作用,您应该设置一个断点并检查密切相关的方法调用的结果,例如 getFillBackgroundColorColor()
The following is for XSSF and is in Scala but it does show exactly how to get the colour from the object model. I wanted to instantiate a java.awt.Color object from the actual rgb values (which is useful partly because my debugger displays for me the actual colour of the object when I stop at breakpoints, and partly because this is for export to systems that have nothing to do with Excel). I'm ignoring the colour's alpha value and my Scala may be a bit naive. I'd suggest that if this doesn't work for you, you should set a break-point and examine the result of closely related method calls such as getFillBackgroundColorColor()
对于XSSF读取xlsx文件(也尝试过HSSF),经过一段时间的挣扎,我刚刚发现
getFillBackgroundXSSFColor()
方法实际上返回了“格式单元格”的“填充”选项卡中的“图案颜色” Excel,而不是该选项卡中所谓的“背景”颜色。我不确定这是否符合预期。请参阅我下面的屏幕截图。返回的RGB实际上是FF0000,即RED。
所以现在,我没有办法解决这种情况,只是要求用户也填充“图案颜色”。
For XSSF reading xlsx file (tried the HSSF as well) ,after struggle for a while, i just found
getFillBackgroundXSSFColor()
method actually returned the "Pattern Color" in the Fill tab of "Format Cells" in Excel, not the so-called "Background" color in that tab. I am not sure if this expected.See my below screenshot. The returned RGB is actually FF0000 ,i.e. RED.
So right now, I do not have a way for this case and just request user to fill the "Pattern Color" as well.