使用SXSSFWorkbook创建groupRows

时间:2017-02-27 18:17:22

标签: java excel apache-poi grouping

阅读https://poi.apache.org/apidocs/org/apache/poi/xssf/streaming/SXSSFSheet.html#groupRow(int,%20int)

它表示应该在窗口大小内创建组,但是如何创建超过窗口大小的组

1 个答案:

答案 0 :(得分:0)

这个最小的例子在一行1000行,窗口大小为200

的情况下,从第2行到第554行生成groupRow
public static final int ROWS=1000;
public static final int WINDOW_SIZE=100;
//public static final int WINDOW_SIZE=1000;

public static void main(String args[]) throws FileNotFoundException, IOException{
    File outputFile = new File("testFile-actual.xlsx");
    SXSSFWorkbook sworkbook = new SXSSFWorkbook(WINDOW_SIZE);
    creatXLSX(outputFile, sworkbook);

    File expectedoutputFile = new File("testFile-expected.xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook();
    creatXLSX(expectedoutputFile, workbook);
}

private static void creatXLSX(File outputFile, Workbook workbook)
    throws IOException, FileNotFoundException {
    Sheet sheet = workbook.createSheet();
    int posFirstRow = 1;
    boolean addgroup = true;
    for(int i = 0; i < ROWS; i++) {
        Row row = sheet.createRow(i);
        Cell cell = row.createCell(0);
        cell.setCellValue(i);
        if (addgroup && i % WINDOW_SIZE == 0) {
            sheet.groupRow(posFirstRow, i);
            posFirstRow = i+1;
        }
        if (i == 556) {
            sheet.groupRow(posFirstRow, 553);
            posFirstRow = 556;
            addgroup = false;
        }
    }
    workbook.write(new FileOutputStream(outputFile));
}

关键是:

groupRow(1,n)
groupRow(n+1, m)

加入两个组并生成相同的

groupRow(1,m) 

此处有更多信息https://bz.apache.org/bugzilla/show_bug.cgi?id=58077