来自十六进制代码的 Apache POI XSSFColor [英] Apache POI XSSFColor from hex code

查看:48
本文介绍了来自十六进制代码的 Apache POI XSSFColor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将单元格的前景色设置为十六进制代码中的给定颜色.例如,当我尝试将其设置为红色时:

I want to set the foreground color of a cell to a given color in hex code. For example, when I try to set it to red:

style.setFillForegroundColor(new XSSFColor(Color.decode("#FF0000")).getIndexed());

无论我在解码函数的参数中设置什么十六进制值,getIndexed 函数将始终返回黑色.

No matter what Hex value I set in the parameter for the decode function, the getIndexed function will always return the black color.

会不会是我做错了什么?我认为这是一个错误,但我不确定...

Could it be that I might be doing something wrong? I think it's a bug but I'm not sure...

推荐答案

好消息是,如果您使用的是 XSSF,而不是 HSSF,那么解决您的问题相当容易.您只需将样式变量转换为 XSSFCellStyle.如果你这样做了,那么有一个版本的 setFillForegroundColor 接受一个 XSSFColor 参数,所以你不需要调用 getIndexed().下面是一些示例代码:

The good news is, if you are using XSSF, as opposed to HSSF, then the solution to your problem is fairly easy. You simply have to cast your style variable to XSSFCellStyle. If you do, then there is a version of setFillForegroundColor that takes an XSSFColor argument, so you need not call getIndexed(). Here is some example code:

XSSFCellStyle style = (XSSFCellStyle)cell.getCellStyle();
XSSFColor myColor = new XSSFColor(Color.RED);
style.setFillForegroundColor(myColor);

但是,如果您使用的是 HSSF,那么事情就更难了.HSSF 使用调色板,它只是一组颜色.您传递给 setFillForegroundColor 的短值是调色板的索引.

However, if you are using HSSF, then things are harder. HSSF uses a color palette, which is simply an array of colors. The short value that you pass into setFillForegroundColor is an index into the palette.

所以您遇到的问题是将 rgb 值转换为调色板索引.您使用 getIndexed() 提出的解决方案是合乎逻辑的,但不幸的是,它确实像您认为的那样适用于 XSSFColor.

So the problem you have is converting an rgb value into a palette index. The solution you proposed, using getIndexed(), is logical, but, unfortuntately, it does work for XSSFColor the way you might suppose it should.

幸运的是,有一个解决方案.目前,让我们假设您会满意使用默认调色板中的一种颜色,而不是使用自定义颜色.在这种情况下,您可以使用 HSSFPalette 和 HSSFColor 类来解决问题.下面是一些示例代码:

Fortunately, there is a solution. For the moment, let us assume you will be satisfied using one of the colors in the default palette, rather than using a custom color. In that case, you can use the HSSFPalette and HSSFColor classes to solve the problem. Here is some example code:

HSSFWorkbook hwb = new HSSFWorkbook();
HSSFPalette palette = hwb.getCustomPalette();
// get the color which most closely matches the color you want to use
HSSFColor myColor = palette.findSimilarColor(255, 0, 0);
// get the palette index of that color 
short palIndex = myColor.getIndex();
// code to get the style for the cell goes here
style.setFillForegroundColor(palIndex);

如果您想使用默认调色板中尚未包含的自定义颜色,则必须将它们添加到调色板中.HSSFPalette 的 javadoc 定义了您可以使用的方法.

If you want to use custom colors not already in the default palette, then you have to add them to the palette. The javadoc for HSSFPalette defines the methods you can use for doing so.

这篇关于来自十六进制代码的 Apache POI XSSFColor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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