问题
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.
回答1:
You can do 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.
Instead you can do: whiteFont.setColor(HSSFColor.WHITE.index)
or whiteFont.setColor(IndexedColors.WHITE.index);
(this is deprecated)
or if you do whiteFont.setColor(new XSSFColor(new Color(255,255,254)));
it'll be really close to white.
回答2:
XSSFFont font = (XSSFFont) wb.createFont();
font.setColor(new XSSFColor( Color.decode("#7CFC00")));
来源:https://stackoverflow.com/questions/31080750/setting-custom-font-color-for-xssfworkbook-in-apache-poi