无法设置填充颜色Apache POI Excel工作簿

我一遍又一遍地扫描了这个论坛并尝试了这里提到的所有方法,但仍然无法让Apache POI更改以填充我的Excel文档的背景颜色。

这是我的代码:

errorOccured = true; XSSFCellStyle cs = workbook.createCellStyle(); cs.setFillBackgroundColor(IndexedColors.RED.getIndex()); row.getCell(0).setCellStyle(cs); 

你知道为什么这不起作用吗? 将row.getCell(0)填充为红色(背景色)的正确方法是什么?

谢谢!

使用forground颜色而不是Background颜色。

  errorOccured = true; XSSFCellStyle style = workbook.createCellStyle(); style.setFillForegroundColor(IndexedColors.RED.getIndex()); style.setFillPattern(FillPatternType.SOLID_FOREGROUND); row.getCell(0).setCellStyle(style); 

这将用RED填充单元格背景颜色。