【发布时间】:2017-05-10 15:17:43
【问题描述】:
我必须打开现有的 Excel 并设置一些值或更改该单元格的背景/前景颜色。
这个 Excel 是关于公司的工作日,所以我已经有一个 Excel 样式。 我必须更改某些单元格的前景,因为您可以想象,在某些日子里我们不工作。
因此,对于每一天(对应于一个单元格),如果它没有“启用”它就必须变成黄色。
我的代码是:
for(int i=0;i<listDays.size();i++) {
int indiceColonna = 6+i;
/* Setto la cella GIALLA relativa alla desc corta giorno */
if(!listDays.get(i).isEnabled()) {
CellStyle cellaGialla = foglio.getRow(5).getCell(indiceColonna).getCellStyle();
cellaGialla.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
cellaGialla.setFillPattern(FillPatternType.SOLID_FOREGROUND);
foglio.getRow(5).getCell(indiceColonna).setCellStyle(cellaGialla);
/* Colonne gialle : da G8 a G18 - a scendere */
for (int y = 0; y < 11; y++) {
Cell cella = foglio.getRow(7 + y).getCell(6 + i);
CellStyle cella2Gialla = cella.getCellStyle();
cella2Gialla.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());
cella2Gialla.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cella.setCellStyle(cella2Gialla);
}
}
}
我想采用单元格的实际 CellStyle 并仅更改前景色。 此代码部分有效,因为也在其他列设置前景色: wrong columns
如何避免此错误? 我使用的是 Apache POI 3.16,但我尝试了一些旧版本。
谢谢!
【问题讨论】:
-
创建一个新的单元格样式,设置样式并应用它?单元格样式是 Excel 中的工作簿范围
标签: excel apache apache-poi foreground