问题描述
我在为 Apache POI
的 XSSFWorkbook
设置自定义字体颜色时遇到了一些麻烦.当我这样做时:
I'm having a little trouble with setting a custom font color for an XSSFWorkbook
from Apache POI
. When I do:
yellow = workbook.createCellStyle();
Font whiteFont = workbook.createFont();
whiteFont.setColor(new XSSFColor(new Color(255, 255, 255)).getIndexed());
yellow.setFillForegroundColor(new XSSFColor(yellowRGB));
yellow.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND);
yellow.setFont(whiteFont);
字体保持黑色,但是我不确定自己在做什么错.
The font stays black, I'm not sure what I'm doing wrong though.
推荐答案
您可以执行 whiteFont.setColor(new XSSFColor(new Color(255,255,255)));
但是,Apache POI中有一个错误,正在切换黑白.看来他们在 XSSFColor.java (请查看XSSFColor.correctRGB()),以解决Excel中的问题.Excel可能已修复,但Apache POI尚未更新.
However, there is a bug in Apache POI, where it is switching black and white. It looks like they put a 'fix' in XSSFColor.java (look at XSSFColor.correctRGB()) to correct for a problem in Excel. It's likely that Excel was fixed, but Apache POI wasn't updated.
您可以执行以下操作: whiteFont.setColor(HSSFColor.WHITE.index)
或 whiteFont.setColor(IndexedColors.WHITE.index);
(已弃用)
Instead you can do: whiteFont.setColor(HSSFColor.WHITE.index)
or whiteFont.setColor(IndexedColors.WHITE.index);
(this is deprecated)
或者如果您执行 whiteFont.setColor(new XSSFColor(new Color(255,255,254)));
它将非常接近白色.
or if you do whiteFont.setColor(new XSSFColor(new Color(255,255,254)));
it'll be really close to white.
这篇关于在Apache POI中为XSSFWorkbook设置自定义字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!