Java Excel automation单元格背景色重复setFillForegroundColor
嘿,下面的代码将单元格背景更改为红色或绿色。当我注释掉绿色的else代码时,我的excel工作表第1行的每个单元格框似乎都是红色的。同样,如果我做相反的操作,注释掉红色和取消注释绿色,那么第1行中的所有单元格都是绿色的 我不明白下面代码中是什么使所有单元格的颜色相同,即使前两个单元格应为红色,而其他所有单元格应为绿色。我已经检查了我的逻辑,它将进入第一个IF2次,然后其余的将进入else,因此这是正确的 我的代码:Java Excel automation单元格背景色重复setFillForegroundColor,java,excel,automation,apache-poi,poi-hssf,Java,Excel,Automation,Apache Poi,Poi Hssf,嘿,下面的代码将单元格背景更改为红色或绿色。当我注释掉绿色的else代码时,我的excel工作表第1行的每个单元格框似乎都是红色的。同样,如果我做相反的操作,注释掉红色和取消注释绿色,那么第1行中的所有单元格都是绿色的 我不明白下面代码中是什么使所有单元格的颜色相同,即使前两个单元格应为红色,而其他所有单元格应为绿色。我已经检查了我的逻辑,它将进入第一个IF2次,然后其余的将进入else,因此这是正确的 我的代码: static CellStyle headerCellStyle = wo
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);
}
静态CellStyle headerCellStyle=workbook.createCellStyle();
对于(int i=0;i单元格填充存储在单元格样式中,而单元格填充存储在工作簿级别。因此,永远不要在将单元格值设置到图纸中的同一循环中创建单元格样式 如果需要两种不同的单元格填充(一种为红色,另一种为绿色实体图案),则还需要两种单元格样式。这些需要首先在工作簿级别创建,然后在设置单元格值的循环中设置为单元格样式 你的代码不完整,所以我只能猜测,你到底想实现什么 我希望以下最小但完整的示例将对您有所帮助:
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();
}
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类CreateExcelCellStyledAndGreen{
公共静态void main(字符串[]args)引发异常{
//工作簿=新的XSSF工作簿();
工作簿=新的HSSF工作簿();
CellStyle HeaderCellStyled=工作簿.createCellStyle();
CellStyle headerCellStyleGreen=工作簿.createCellStyle();
headerCellStyled.setWrapText(true);
HeaderCellStyled.setFillPattern(FillPatternType.SOLID\u前景);
HeaderCellStyled.setAlignment(水平对齐.JUSTIFY);
HeaderCellStyled.设置垂直对齐(垂直对齐.中心);
headerCellStyleGreen.cloneStyleFrom(headerCellStyleRed);
HeaderCellStyled.setFillForegroundColor(IndexedColors.RED.getIndex());
headerCellStyleGreen.setFillForegroundColor(IndexedColors.GREEN.getIndex());
字符串[]arr=新字符串[]{
“文件名.xml”,
“B文件名.xml*”,
“C文件名.xml”,
“D文件名.xml”,
“E文件名.xml*”,
“F文件名.xml”
};
int rowNum=1;
int colNum=1;
工作表=工作簿.createSheet();
对于(int i=0;i
单元格填充存储在单元格样式中,而单元格填充则存储在工作簿级别。因此,永远不要在将单元格值设置到图纸中的同一循环中创建单元格样式
如果需要两种不同的单元格填充(一种为红色,另一种为绿色实体图案),则还需要两种单元格样式。这些需要首先在工作簿级别创建,然后在设置单元格值的循环中设置为单元格样式
你的代码不完整,所以我只能猜测,你到底想实现什么
我希望以下最小但完整的示例将对您有所帮助:
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();
}
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类CreateExcelCellStyledAndGreen{
公共静态void main(字符串[]args)引发异常{
//工作簿=新的XSSF工作簿();
工作簿=新的HSSF工作簿();
CellStyle HeaderCellStyled=工作簿.createCellStyle();
CellStyle headerCellStyleGreen=工作簿.createCellStyle();
headerCellStyled.setWrapText(true);
HeaderCellStyled.setFillPattern(FillPatternType.SOLID\u FOREGROUN