我尝试了在stackoverflow上发布的不同解决方案,以将背景颜色应用于Apache POI生成的单元格,但没有任何作用。
我正在做类似以下的事情:
我认为我的代码没问题,但是每个被这样样式化的单元格都会有黑色背景。
我对“DefaultIndexedColorMap”实例有些疑问,在调试时结果没有字段。
此时,我不确定该做什么来解决问题。其他帖子中的每个人似乎都能让事情正常运作,但我仍然得到暗背景而不是黄色。
我正在做类似以下的事情:
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);
我认为我的代码没问题,但是每个被这样样式化的单元格都会有黑色背景。
![black-cells](https://istack.dev59.com/a33Ut.webp)
有什么建议吗? 提前致谢!