Java POI仅为前50行添加单元格背景
我之前已经生成了.xls(也是由ApachePOI生成的),再次打开并按单元格值更改背景颜色。问题是,所有单元格的背景都并没有改变,只改变了大约前50行的背景,其他的保留了白色背景。函数Java POI仅为前50行添加单元格背景,java,apache-poi,Java,Apache Poi,我之前已经生成了.xls(也是由ApachePOI生成的),再次打开并按单元格值更改背景颜色。问题是,所有单元格的背景都并没有改变,只改变了大约前50行的背景,其他的保留了白色背景。函数mark()在一个for循环中,我尝试转储值、行号,最后尝试一次又一次地迭代xls assing color,但没有任何效果 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Row; imp
mark()
在一个for循环中,我尝试转储值、行号,最后尝试一次又一次地迭代xls assing color,但没有任何效果
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Font;
public class Xls {
private FileOutputStream fileOut;
private Sheet xlsSheet;
private HSSFWorkbook xlsWorkbook;
private CellStyle cellStyle;
public Xls(String path) {
try {
fileOut = new FileOutputStream(path);
this.xlsWorkbook = new HSSFWorkbook();
this.xlsSheet = xlsWorkbook.createSheet("test");
cellStyle = this.xlsWorkbook.createCellStyle();
Row row1 = xlsSheet.createRow((int) 0);
this.xlsSheet.autoSizeColumn(0, true);
this.xlsSheet.autoSizeColumn(1, true);
this.xlsSheet.autoSizeColumn(2, true);
this.xlsSheet.autoSizeColumn(3, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public void mark(int rowNumber, String status) {
short color = Constants.getColor(status);
CellStyle style = this.xlsWorkbook.createCellStyle();
style.setFillForegroundColor(color);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
//TODO!!! Why filling color only in first 50 rows?!
// System.out.println("Changing row " + sameRows.get(rowNumber) + " status is:'" + status + "' color:" + color);
Row row = xlsSheet.getRow(sameRows.get(rowNumber));
Cell statusCell = null;
if (!isCellEmpty(row.getCell(3))) {
statusCell = row.getCell(3);
} else {
statusCell = row.createCell(3);
}
statusCell.setCellValue(status);
statusCell.setCellStyle(style);
}
}
}
你知道哪里会出错吗?
注意:
setCellValue()
正在工作-所有字段都有正确的值。不应为每个单元格重新创建单元格样式,它们是Excel文件中的有限资源(限制由Excel本身施加),因此,只需创建一次样式对象,并将其用于所有应具有相同样式的单元格。不应为每个单元格重新创建单元格样式,它们在Excel文件中是有限的资源(限制由Excel本身施加),因此,只需创建一次样式对象,并对所有应具有相同样式的单元格重复使用它。谢谢,我有更多的颜色,因此我创建了几个变量及其工作原理;-)谢谢,我有更多的颜色,所以我只创建了几个变量及其工作;-)