alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

同一个Excel 不能追加Sheet #236

Closed shanjungit closed 5 years ago

shanjungit commented 5 years ago

我按照WriteTest里的方法, new Sheet(x,0), 我本来希望可以,通过ExcelReader读取到所有的Sheet页数量,然后在后面追加,可以每次我 new Sheet(x+1,0) 再使用ExcelWriiter 写入的时候,都把原来的覆盖掉了。 导致了,每次我的Excel都只有一个Sheet,就是最后写入的sheet. 这可能是什么情况?

shanjungit commented 5 years ago

这样子写可以追加sheet
@Test public void writeV2019() throws IOException { OutputStream out = new FileOutputStream("E:\2019.xlsx"); ExcelWriter writer = EasyExcelFactory.getWriter(out);

    for(int i = 0; i < 14;i++) {
        Sheet tempSheet = new Sheet(i, 0);
        tempSheet.setSheetNo(i);
        tempSheet.setSheetName("第"+i+"Sheet");
        writer.write1(createTestListObject(), tempSheet);
    }
    writer.finish();
    out.close();
}

下面的写法,就不能追加了 for(int i = 0; i < 14;i++) { OutputStream out = new FileOutputStream("E:\2019.xlsx"); ExcelWriter writer = EasyExcelFactory.getWriter(out); Sheet tempSheet = new Sheet(i, 0); tempSheet.setSheetNo(i); tempSheet.setSheetName("第"+i+"Sheet"); writer.write1(createTestListObject(), tempSheet); writer.finish(); out.close(); }

Bin-mario commented 5 years ago

建议 巩固一下 文件流的基础知识,循环中每次 新建一个流,又没有使用追加模式,肯定是覆盖

LanceXuan commented 5 years ago

请问一下 new Sheet(x,0),这个方法的第二个参数HeadLineMun是什么意思呢

zhuangjiaju commented 5 years ago

这个已经在最新版本修复,请使用最新版本。https://maven-badges.herokuapp.com/maven-central/com.alibaba/easyexcel。 并且参照:https://github.com/alibaba/easyexcel/blob/master/quickstart.md 去书写