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

问题描述 投票:28回答:1

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

这是我的代码:

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

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

谢谢!

java colors background apache-poi fill
1个回答
59
投票

使用前景色而不是背景色。

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

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

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.