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);
}
}