由于某种原因,Apache POI Cellstyle在第41行之后不适用

时间:2017-09-13 08:06:24

标签: java excel apache-poi

正如你可以从标题中理解的那样,在第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语句中经过。但是当我查看excel文件时,它们似乎没有任何属性。这就是我称这个功能的部分:

 int rowCount = 3;
 for (Table table : tableList){
        Row row = sheet.createRow(++rowCount);
        writeInterlock(table,row,workbook);
    }

我不知道发生了什么,所以任何帮助都会受到高度赞赏

2 个答案:

答案 0 :(得分:1)

工作簿中唯一单元格格式/单元格样式的最大数量有Excel limit

所以为每一行创建一个单元格样式。据我所知,你只需要两种不同的细胞样式。因此,将这两个创建为cellStyle1cellStyle2 外部方法:

...
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
 }
 ...
}

答案 1 :(得分:0)

你所接受的答案是完全正确的,有一个限制,好吧,但是问题发生在你身上,因为你正在使用excel 97'( - 2007)的实现,这里有一个{{3} }。

如果您真的不需要 .xls 格式的文件,请在静态下不需要管理变量的工作示例(与您的类似)下方方式就像你接受的答案一样。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class MainWriteCells {

    public MainWriteCells() {
        // TODO Auto-generated constructor stub
    }

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        FileInputStream is = new FileInputStream(new File("C:\\ook.xlsx"));
        XSSFWorkbook wb = new XSSFWorkbook(is);
        XSSFSheet sheet = wb.getSheet("Sheet1");

        for(int i=0;i<1000;i++){
            writeTable(sheet.createRow(i),wb);
        }
        wb.write(new FileOutputStream(new File("C:\\ook.xlsx")));

    }

    private static void writeTable(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("TODO");
        cell.setCellStyle(cellStyle);


        cell = row.createCell(1);
        cell.setCellValue("TODO");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(2);
        cell.setCellValue("TODO");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(3);
        cell.setCellValue("TODO");
        cell.setCellStyle(cellStyle);

        cell = row.createCell(4);
        cell.setCellValue("TODO");
        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);
    }

}

如您所见,我在 .xslx

中更改了两个对象和文件扩展名
XSSFWorkbook wb = new XSSFWorkbook(is);
    XSSFSheet sheet = wb.getSheet("Sheet1");

而不是

HSSFWorkbook wb = new HSSFWorkbook(is);
        HSSFSheet sheet = wb.getSheet("Sheet1");