I want to use a custom RGB color (38,38,38) for my cell backgroundcolor. For that I use this code:
IndexedColorMap colorMap = wb.getStylesSource().getIndexedColors();
XSSFColor customtablegrey = new XSSFColor(new java.awt.Color(38,38,38), colorMap);
cellFormat1.setFillForegroundColor(customtablegrey.getIndex());
cellFormat1.setFillPattern(FillPatternType.SOLID_FOREGROUND);
But all I get is a black background. Why and how can I change it?
Best Regards,
Christian
CodePudding user response:
That's true. IndexedColorMap
is useless for XSSFColor
s. XSSFColor
s are not indexed colors. In Office Open XML
custom colors are stored as RGB directly in the XML. They are not stored in any kind of color map.
Setting XSSFColor
s as fill foreground color to cell styles is possible using XSSFCellStyle.setFillForegroundColor(XSSFColor color) only. It cannot be set using CellStyle.setFillForegroundColor(short fg)
.
So cellFormat1.setFillForegroundColor(customtablegrey)
should work when cellFormat1
is a XSSFCellStyle
.
How to use colors not in IndexedColors for Apache POI XSSF Excel? shows a complete example for how to use XSSFColor
as cell fill color. Just tested using apache poi 5.1.0
too.
APACHE POI 4.1 : Set cell background color from hex code shows another complete example. Also tested and works using apache poi 5.1.0
too.