I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.
I'm doing something like:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet(sheetName);
XSSFCellStyle cellStyle = ((XSSFCellStyle) workbook.createCellStyle());
if (styleObject.getBgColor() != null) {
java.awt.Color javaBdgColor = java.awt.Color.decode(voceStyle.getBgColor()); // this is #FFF000
XSSFColor bgColor = new XSSFColor(javaBdgColor, new DefaultIndexedColorMap());
cellStyle.setFillForegroundColor(bgColor.getIndex());
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}
Row newRow = Rowsheet.createRow(0);
Cell newCell = newRow.createCell(0);
newCell.setCellStyle(cellStyle);
// write file
String pathFileExport = buildPathExportFile("test-export");
FileOutputStream fileOut = new FileOutputStream(pathFileExport);
workbook.write(fileOut);
fileOut.close();
//close workbook
workbook.close();
return Paths.get(pathFileExport);
I think everything is ok in my code but every cell styled like that will result in a black background.
I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:
At this point, I'm not sure about what to do to solve. Everyone in other posts seems to get things working but I'm still getting dark backgrounds instead of yellow.
Any suggestions? Thanks in advance!