Setting foreground color for HSSFCellStyle is always coming out black

后端 未结 3 624
旧时难觅i
旧时难觅i 2020-12-05 04:09

I am using POI to create an Excel spreadsheet in Java. I have the following code used for creating a header row:

HSSFWorkbook wb = new HSSFWorkbook();
HSSFSh         


        
相关标签:
3条回答
  • 2020-12-05 04:35

    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.

    0 讨论(0)
  • 2020-12-05 04:47

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

    0 讨论(0)
  • 2020-12-05 04:51

    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!

    0 讨论(0)
提交回复
热议问题