从标题中可以看出,在第41行之后,即使调试后,我仍然看到我的代码运行良好,但我的样式仍然不适用。
我的职能是:
private void writeTable(Table table,Row row,Workbook wb){
CellStyle cellStyle = wb.createCellStyle();
if(row.getRowNum() % 2 == 0) {
cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setWrapText(true);
cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
}else{
cellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
cellStyle.setAlignment(CellStyle.ALIGN_CENTER);
cellStyle.setBorderTop(CellStyle.BORDER_THIN);
cellStyle.setWrapText(true);
cellStyle.setTopBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderBottom(CellStyle.BORDER_THIN);
cellStyle.setBottomBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderRight(CellStyle.BORDER_THIN);
cellStyle.setRightBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
cellStyle.setBorderLeft(CellStyle.BORDER_THIN);
cellStyle.setLeftBorderColor(IndexedColors.GREY_80_PERCENT.getIndex());
}
Cell cell = row.createCell(0);
cell.setCellValue(Table.index);
cell.setCellStyle(cellStyle);
cell = row.createCell(1);
cell.setCellValue(strCorrecter(Table.Name).isEmpty() ? "-" : strCorrecter(Table.Name));
cell.setCellStyle(cellStyle);
cell = row.createCell(2);
cell.setCellValue(strCorrecter(Table.Surname.toString()).isEmpty() ? "-" : strCorrecter(Table.Surname.toString()));
cell.setCellStyle(cellStyle);
cell = row.createCell(3);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(4);
cell.setCellValue(strCorrecter(Table.Age.toString()).isEmpty() ? "-" : strCorrecter(Table.Age.toString()));
cell.setCellStyle(cellStyle);
cell = row.createCell(5);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell =row.createCell(6);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(7);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(8);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(9);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(10);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(11);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(12);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(13);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
cell = row.createCell(14);
cell.setCellValue("TODO");
cell.setCellStyle(cellStyle);
}
而且我看到所有行都通过函数开头的if else语句。但是当我查看excel文件时,它们似乎没有任何属性。这就是我调用此功能的部分:
int rowCount = 3;
for (Table table : tableList){
Row row = sheet.createRow(++rowCount);
writeInterlock(table,row,workbook);
}
我不知道发生了什么,所以任何帮助将不胜感激
最佳答案
一个Excel limit表示工作簿中唯一单元格格式/单元格样式的最大数量。
因此,请勿为每一行创建单元格样式。据我所知,您只需要两种不同的单元格样式。因此,在方法之外将这两个分别创建为cellStyle1
和cellStyle2
:
...
CellStyle cellStyle1 = wb.createCellStyle();
//set all the needed settings
CellStyle cellStyle2 = wb.createCellStyle();
//set all the needed settings
...
然后只在方法中使用两者:
private void writeTable(Table table,Row row,Workbook wb) {
...
if(row.getRowNum() % 2 == 0) {
//here use cellStyle1
} else {
//here use cellStyle2
}
...
}