Setting custom font color for XSSFWorkbook in Apache POI

 ̄綄美尐妖づ 提交于 2021-02-18 20:59:40

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!