Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting custom font color for XSSFWorkbook in Apache POI

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.

like image 938
silverAndroid Avatar asked Jun 26 '15 19:06

silverAndroid


2 Answers

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.

like image 53
Phill Treddenick Avatar answered Sep 21 '22 09:09

Phill Treddenick


XSSFFont font = (XSSFFont) wb.createFont();
font.setColor(new XSSFColor( Color.decode("#7CFC00")));
like image 37
Lin W Avatar answered Sep 19 '22 09:09

Lin W