gpt4 book ai didi

Java Excel自动化单元格背景颜色重复setFillForegroundColor

转载 作者:行者123 更新时间:2023-12-02 01:16:58 25 4
gpt4 key购买 nike

嘿,我有下面的代码,可以将单元格的背景更改为红色绿色。似乎当我注释掉 GREEN else 代码时,我的 Excel 工作表中第 1 行中的每个单元格框都为红色。同样,如果我执行相反的操作并注释掉 RED 并取消注释 GREEN,则第 1 行中的所有单元格均为绿色。

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

我的代码:

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

codeguru

最佳答案

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

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

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

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

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();
}
}

关于Java Excel自动化单元格背景颜色重复setFillForegroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58415884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com