简体   繁体   English

如何在apache-poi的新版本中为单个单元格设置Excel单元格前景色?

[英]How to set an Excel Cell foreground color for individual cells in the new version for apache-poi?

I am using the new version for poi – 3.11 我正在使用新版本的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. 下面的代码将Red设置为整个工作表的前景色,但是我需要各种单元格颜色。 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);
        }
    }
}

The comment of Axel Richter practically gave the solution. Axel Richter的评论实际上给出了解决方案。 So, the refined code is as follows: 因此,改进后的代码如下:

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet sheet = workbook.createSheet("Calendar");
    CellStyle styleWhite = workbook.createCellStyle();
    styleWhite.setFillForegroundColor(IndexedColors.WHITE.getIndex());
    styleWhite.setFillPattern(CellStyle.SOLID_FOREGROUND);

    CellStyle styleYellow = workbook.createCellStyle();
    styleYellow.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
    styleYellow.setFillPattern(CellStyle.SOLID_FOREGROUND);

    CellStyle styleRed = workbook.createCellStyle();
    styleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
    styleRed.setFillPattern(CellStyle.SOLID_FOREGROUND);

    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]);
            cell.setCellStyle(styleWhite);
            if (res[rowNum][colNum] == CellEnum.BUSY.getValue()) {
                cell.setCellStyle(styleRed);
            } else if (res[rowNum][colNum] == CellEnum.Pass.getValue()) {
                cell.setCellStyle(styleYellow);
            } else {
                cell.setCellStyle(styleWhite);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM