Java Excel 自动化单元格背景颜色重复设置填充前景色



嘿,我下面的代码将单元格的背景更改为红色绿色。似乎当我注释掉绿色的else代码时,我的excel工作表在第1行的每个单元格框都为红色。同样,如果我做相反的事情并注释掉RED并取消注释GREEN,那么第 1 行中的所有单元格都是绿色的。

我不明白下面的代码中是什么让它为所有单元格着色相同的颜色,即使前 2 个单元格应该是红色的,而所有其他单元格都应该是绿色的。我已经检查了我的逻辑,它转到第一个IF2 次,然后其余的转到其他,所以这是正确的。

我的代码:

static CellStyle headerCellStyle    = workbook.createCellStyle();
for (int i = 0; i < arr.length; i++) {
Row row             = sheet.createRow(rowNum1++);
HSSFWorkbook hwb    = new HSSFWorkbook();
HSSFPalette palette = hwb.getCustomPalette();
Cell cell           = row.createCell(colNum);
headerCellStyle.setWrapText(true);
headerCellStyle.setAlignment(HorizontalAlignment.LEFT);
headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerCellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
if (arr[i].contains("*")) {
//RED
headerCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
cell.setCellValue(arr[i].replace(" " + (i + 1) + ".xml*", ""));
} else {
//GREEN
headerCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
cell.setCellValue(arr[i].replace(" " + (i + 1) + ".xml", ""));
}
row.getCell(0).setCellStyle(headerCellStyle);
row.setHeightInPoints(20);
}

我确定我只是在看一些非常明显的东西,但目前我无法找到那可能是什么。

任何帮助都会很棒!

注意:也发布到以下论坛:

coderanch.com

密码大师

单元格填充存储在单元格样式中,而这些样式存储在工作簿级别。因此,切勿在将单元格值设置到工作表中的同一循环中创建单元格样式。

如果需要两个不同的单元格填充(一个具有红色,一个具有绿色实心图案(,则还需要两种单元格样式。这些需要首先在工作簿级别创建,然后在设置单元格值的循环中设置为单元格样式。

你的代码不完整,所以我只能猜测,你到底想实现什么。

我希望以下最小但完整的示例对您有所帮助:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateExcelCellStyleRedAndGreen {
public static void main(String[] args) throws Exception {
//Workbook workbook = new XSSFWorkbook();
Workbook workbook = new HSSFWorkbook();
CellStyle headerCellStyleRed = workbook.createCellStyle();
CellStyle headerCellStyleGreen = workbook.createCellStyle();
headerCellStyleRed.setWrapText(true);
headerCellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);
headerCellStyleRed.setAlignment(HorizontalAlignment.JUSTIFY);
headerCellStyleRed.setVerticalAlignment(VerticalAlignment.CENTER);
headerCellStyleGreen.cloneStyleFrom(headerCellStyleRed);
headerCellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
headerCellStyleGreen.setFillForegroundColor(IndexedColors.GREEN.getIndex());
String[] arr = new String[] {
"A Name of File.xml", 
"B Name of File.xml*", 
"C Name of File.xml", 
"D Name of File.xml", 
"E Name of File.xml*", 
"F Name of File.xml"
};
int rowNum=1;
int colNum=1;
Sheet sheet = workbook.createSheet();
for (int i = 0; i < arr.length; i++) {
Row row = sheet.createRow(rowNum++);
Cell cell = row.createCell(colNum);
if (arr[i].contains("*")) {
//RED
cell.setCellStyle(headerCellStyleRed);
cell.setCellValue(arr[i].replace(".xml*", ""));
} else {
//GREEN
cell.setCellStyle(headerCellStyleGreen);
cell.setCellValue(arr[i].replace(".xml", ""));
}
row.setHeightInPoints(50);
}
FileOutputStream out = null;
if (workbook instanceof HSSFWorkbook) {
out = new FileOutputStream("CreateExcelCellStyleRedAndGreen.xls");
} else if (workbook instanceof XSSFWorkbook) {
out = new FileOutputStream("CreateExcelCellStyleRedAndGreen.xlsx");
}
workbook.write(out);
out.close();
workbook.close();
}
}

最新更新