Apache的POI XSSFColor从十六进制code [英] Apache POI XSSFColor from hex code

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

问题描述

欲小区的前景颜色设置为十六进制code给定的颜色。例如,当我尝试将它设置为红色:

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

不管是十六进制数值我在去code函数参数设置,则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()。下面是一些例子code:

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(),是合乎逻辑的,但是,unfortuntately,但它确实工作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类来解决这个问题。下面是一些例子code:

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从十六进制code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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