I am using the new version for poi – 3.11
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.11</version>
</dependency>
I found that the old code for setting a foreground color is not compiled anymore but my new code does not work either. The code below sets Red as foreground color for the whole worksheet, but I need various cell colors. Cell values are set correctly.
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("Calendar");
for (int rowNum=0; rowNum<n; rowNum ++)
{
Row row = sheet.createRow(rowNum);
for (int colNum = 0; colNum < m; colNum++)
{
Cell cell = row.createCell(colNum);
cell.setCellValue(grid[rowNum][colNum]);
CellStyle cellStyle = cell.getCellStyle();
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(HSSFColor.WHITE.index);
if (res[rowNum][colNum] == CellEnum.BUSY.getValue())
{
cell.setCellValue(res[rowNum][colNum] + "|" + grid[rowNum][colNum]);
cellStyle.setFillForegroundColor(HSSFColor.RED.index);
}
if (res[rowNum][colNum] == CellEnum.Pass.getValue())
{
cell.setCellValue(res[rowNum][colNum] + "|" + grid[rowNum][colNum]);
cellStyle.setFillForegroundColor(HSSFColor.YELLOW.index);
}
}
}
CellStyle cellStyle = cell.getCellStyle();
withcell
being a new created cell without special style, then you will get the default cell style. You must creating a cell style if you want it be additional to default cell style. See: poi.apache.org/spreadsheet/quick-guide.html#FillsAndFrills. – Axel Richter