POI设置前景色在if语句中不起作用

时间:2016-12-07 17:34:06

标签: java apache-poi

我正在做一些比较两张纸的代码,并输出一个文件,匹配的单元格为绿色,其他单元格为红色。

有问题的部分(请参阅最后的完整代码)

CellStyle style = cellOutputFile.getCellStyle();

if (isCellContentEqual(cellComparisonFile1, cellComparisonFile2) == true)
{
    style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
}
else
{
    style.setFillForegroundColor(IndexedColors.RED.getIndex());
}

style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellOutputFile.setCellStyle(style);

问题

此代码始终将所有单元格显示为绿色,即使在调试时触发了else语句并且样式设置为红色背景。

提前感谢您的帮助!

完整代码

    for (int i = 0; i < workbookComparisonFile1.getNumberOfSheets(); i++)
    {
        sheetComparisonFile1 = workbookComparisonFile1.getSheetAt(i);
        sheetComparisonFile2 = workbookComparisonFile2.getSheet(sheetComparisonFile1.getSheetName());
        sheetOutputFile = workbookOutputFile.getSheet(sheetComparisonFile1.getSheetName());

        System.out.println(sheetComparisonFile1.getLastRowNum());

        if (sheetComparisonFile2 != null)
        {
            for (int j = 0; j < sheetComparisonFile1.getLastRowNum(); j++)
            {
                Row rowComparisonFile1 = sheetComparisonFile1.getRow(j);
                Row rowComparisonFile2 = sheetComparisonFile2.getRow(j);
                Row rowOutputFile = sheetOutputFile.getRow(j);

                if ((rowComparisonFile1 != null && rowComparisonFile2 != null)
                        && (rowComparisonFile1.getLastCellNum() == rowComparisonFile2.getLastCellNum()))
                {
                    for (int k = 0; k < rowComparisonFile1.getLastCellNum(); k++)
                    {
                        Cell cellComparisonFile1 = rowComparisonFile1.getCell(k);
                        Cell cellComparisonFile2 = rowComparisonFile2.getCell(k);
                        Cell cellOutputFile = rowOutputFile.getCell(k);

                        if (cellComparisonFile1 != null && cellComparisonFile2 != null)
                        {
                            CellStyle style = cellOutputFile.getCellStyle();

                            if (isCellContentEqual(cellComparisonFile1, cellComparisonFile2) == true)
                            {
                                style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
                            }
                            else
                            {
                                style.setFillForegroundColor(IndexedColors.RED.getIndex());
                            }

                            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                            cellOutputFile.setCellStyle(style);
                        }
                    }
                }
            }
        }
    }

public boolean isCellContentEqual(Cell cell1, Cell cell2)
{
    if (getCellContents(cell1).equals(getCellContents(cell2)) == false)
    {
        System.out.println("Sheet: " + cell1.getRow().getSheet().getSheetName() +
                " Cell1: " + cell1.getRow().getRowNum() + "-" + cell1.getColumnIndex() + " "
                + getCellContents(cell1).equals(getCellContents(cell2)));
    }
    return getCellContents(cell1).equals(getCellContents(cell2));
}

public String getCellContents(Cell cell)
{
    DataFormatter df = new DataFormatter();

    return df.formatCellValue(cell);
}

1 个答案:

答案 0 :(得分:2)

样式是在工作簿级别而不是单元级别创建的。请尝试以下代码,但如果工作簿样式表已满,则可能会失败。修改内部循环的if / else逻辑:

if (isCellContentEqual(cellComparisonFile1, cellComparisonFile2) == true)
{
    CellStyle style = workbook.createCellStyle();
    style.cloneStyleFrom(cellOutputFile.getCellStyle());
    style.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cellOutputFile.setCellStyle(style);
}
else
{
    CellStyle style = workbook.createCellStyle();
    style.cloneStyleFrom(cellOutputFile.getCellStyle());
    style.setFillForegroundColor(IndexedColors.RED.getIndex());
    style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    cellOutputFile.setCellStyle(style);
}

编辑更新代码示例以在更改填充前景色和填充图案之前复制预先存在的样式