0
我正在使用POI库来处理Excel文件,我想更改特定单元格的ForGroundColor。由于我对IndexedColors列表不满意,我想通过替换已存在的列表(我的情况为HSSFColor.BLUE)来创建自己的列表,问题是 - 它仅保存上次迭代的颜色(所有单元格都相同颜色)。在Excel中动态更改单元格颜色
代码(convData - 两个模糊的双阵列,标准化为255):
HSSFPalette hssfPalette = excelFile.getCustomPalette();
CellStyle cellStyle = excelFile.createCellStyle();
hssfPalette = excelFile.getCustomPalette();
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
for (int i=0; i<convData.length; i++) {
Row row = excelSheet.createRow(i);
for (int j=0; j<convData[i].length; j++) {
Cell cell = row.createCell(j);
hssfPalette.setColorAtIndex(HSSFColor.BLUE.index, convData[i][j].byteValue(), convData[i][j].byteValue(), convData[i][j].byteValue());
cellStyle.setFillForegroundColor(hssfPalette.getColor(HSSFColor.BLUE.index).getIndex());
cell.setCellStyle(cellStyle);
}
}