Setting foreground color for HSSFCellStyle is always coming out black

Solution 1:

I got this to work. I had to set the foreground color to make the background color work (??).

So I changed:

cellStyle.setFillBackgroundColor(HSSFColor.GREY_25_PERCENT.index);

to:

cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);

and it worked!

Solution 2:

If you are setting the foreground color, use

cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

If you are setting the background color, use

style.setFillPattern(FillPatternType.THICK_BACKWARD_DIAG);

or

style.setFillPattern(FillPatternType.THIN_BACKWARD_DIAG);

The foreground and background colors seem to 'stack' (red + blue = purple) if you set the foreground fill pattern before the background fill pattern, but not the other way round. There are several other fill patterns you can choose from. Note that the color will not be applied if you do not change the default fill pattern.

CellStyle.SOLID_FOREGROUND is deprecated in version 3.15+. Use FillPatternType.SOLID_FOREGROUND instead.

Solution 3:

csHeader.setFillForegroundColor(HSSFColor.SKY_BLUE.index); csHeader.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);