使用Apache POI更改行的背景颜色

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

我尝试使用Apache POI来连续改变细胞的背景颜色。我使用以下代码在xls文件中处理它,但执行后文件中没有任何更改。

FileInputStream fis = new FileInputStream(src);
HSSFWorkbook wb = new HSSFWorkbook(fis);                
r = sheet.getRow(5);
CellStyle style = wb.createCellStyle();
style.setFillForegroundColor(IndexedColors.RED.getIndex());
r.setRowStyle(style);
java apache-poi hssf
1个回答
0
投票

细胞的样式必须像这样定义。

HSSFCellStyle tCs = wb.createCellStyle();
tCs.setFillPattern(FillPatternType.SOLID_FOREGROUND);
tCs.setFillForegroundColor(IndexedColors.YELLOW.getIndex());

必须应用每个细胞,这需要这种风格。

for (int k = 0; k < sheet.getRow(5).getLastCellNum(); k++) {
   sheet.getRow(i).getCell(k).setCellStyle(tCs);
}
© www.soinside.com 2019 - 2024. All rights reserved.