使用findSimilarColor设置Excel文件的背景

我在我的java代码中使用findSimilarColor有一个小问题。 我已经阅读了一些从帮助我到下面的代码的计算器的文章。

HSSFCellStyle style = wb.createCellStyle(); HSSFPalette palette = wb.getCustomPalette(); // get the color which most closely matches the color you want to use HSSFColor myColor = palette.findSimilarColor(226, 0, 116); //java don't recognize this color // get the palette index of that color short palIndex = myColor.getIndex(); // code to get the style for the cell goes here style.setFillForegroundColor(palIndex); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION); 

因此,除了使用我试图使用的RGB颜色(226, 0, 116)之外,我没有设置颜色的问题。

出于某种原因,最后打开我的excel文件时出现的颜色是RGB (128, 0, 128)

有没有人有任何想法,为什么会这样呢? 还是另一种解决scheme?

谢谢您的帮助。

对象调色板中是否定义了颜色(226, 0, 116) ? 你要求在调色板中定义的色彩更接近你的要求,似乎(128, 0, 128)是最接近的。

尝试像这样:

 HSSFColor myColor = palette.addColor(226, 0, 116); 

而不是要求相似的颜色。

我只是testing你的解决scheme,我得到“找不到免费的颜色索引”错误。 所以我search一下,找出下面的代码来避免这个错误。

 HSSFPalette palette = wb.getCustomPalette(); palette.setColorAtIndex(HSSFColor.TAN.index, (byte)226, (byte)0, (byte)116); cabecalho.setFillForegroundColor(HSSFColor.TAN.index); 

看起来,我不能添加颜色的调色板,因为调色板已满,所以这个代码我能够覆盖“Tan.index”有我想要的RGB颜色。

我会尽量find一个更好的解决scheme,但同时这将有很大的帮助。

再次感谢你。