java 在 Apache POI 中为 XSSFWorkbook 设置自定义字体颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31080750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting custom font color for XSSFWorkbook in Apache POI
提问by silverAndroid
I'm having a little trouble with setting a custom font color for an XSSFWorkbook
from Apache POI
. When I do:
我在为XSSFWorkbook
from设置自定义字体颜色时遇到了一些麻烦Apache POI
。当我做:
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.
字体保持黑色,但我不确定我做错了什么。
采纳答案by Phill Treddenick
You can do whiteFont.setColor(new XSSFColor(new Color(255,255,255)));
你可以做 whiteFont.setColor(new XSSFColor(new Color(255,255,255)));
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.
但是,Apache POI 中存在一个错误,它会切换黑白。看起来他们在XSSFColor.java 中放置了一个“修复” (查看 XSSFColor.correctRGB())以纠正 Excel 中的问题。Excel 可能已修复,但 Apache POI 未更新。
Instead you can do: whiteFont.setColor(HSSFColor.WHITE.index)
or whiteFont.setColor(IndexedColors.WHITE.index);
(this is deprecated)
相反,您可以执行以下操作:whiteFont.setColor(HSSFColor.WHITE.index)
或whiteFont.setColor(IndexedColors.WHITE.index);
(已弃用)
or if you do whiteFont.setColor(new XSSFColor(new Color(255,255,254)));
it'll be really close to white.
或者如果你这样做,whiteFont.setColor(new XSSFColor(new Color(255,255,254)));
它会非常接近白色。
回答by Lin W
XSSFFont font = (XSSFFont) wb.createFont();
font.setColor(new XSSFColor( Color.decode("#7CFC00")));