![](/img/trans.png)
[英]How to set an Excel Cell foreground color for individual cells in the new version for apache-poi?
[英]POI Set foreground color not working in if statement
我正在做一些代碼,將兩張紙進行比較,並輸出一個文件,其中匹配的單元格為綠色,其他單元格為紅色。
有問題的部分(請參閱最后的完整代碼)
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);
}
樣式是在工作簿級別而不是單元級別創建的。 請嘗試以下代碼,但是如果工作簿樣式表已滿,則可能會失敗。 修改內部循環的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);
}
編輯代碼示例已更新,可以在更改填充前景色和填充圖案之前復制現有樣式
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.