java 如何使用 SXSSF 写入现有文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32029374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:31:40  来源:igfitidea点击:

How to write to an existing file using SXSSF?

javaexcelapache-poixssf

提问by Oliv

I have an .xlsx file with multiple sheets containing different data. Of all the sheets one sheet needs to accommodate close to 100,000 rows of data, and the data needs to be written using Java with poi.

我有一个 .xlsx 文件,其中包含多个包含不同数据的工作表。在所有的sheet中,一张sheet需要容纳接近10万行的数据,数据需要用java和poi来写。

This seems quite fast and simple with SXSSFWorkbook, where I can keep only 100 rows in memory, but the disadvantage is that I can only write to a new file (or overwrite existing file).

对于 SXSSFWorkbook,这似乎非常快速和简单,我只能在内存中保留 100 行,但缺点是我只能写入新文件(或覆盖现有文件)。

Also, I am not allowed to 'load' an existing file, i.e

另外,我不允许“加载”现有文件,即

 SXSSFWorkbook wb = new SXSSFWorkbook(file_input_stream) 
不被允许。

I can use Workbook factory:

我可以使用工作簿工厂:

Workbook workbook = new SXSSFWorkbook();
workbook = WorkbookFactory.create(file_input_stream);

but when the time comes for me to flush the rows,

但是到了我要刷新行的时候

 ((SXSSFSheet)sheet).flushRows(100); 

I get the error that type conversion is not allowed from XSSFSheet to SXSSFSheet.

我收到错误,提示不允许从 XSSFSheet 到 SXSSFSheet 进行类型转换。

I tried to see if there was any way to copy sheets across different workbooks, but so far it seems it has to be done cell by cell.

我试图看看是否有任何方法可以跨不同的工作簿复制工作表,但到目前为止,它似乎必须逐个单元地完成。

Any insights on how to approach this problem?

关于如何解决这个问题的任何见解?

回答by Oliv

You are probably having a template to which you want to add large data. You need to use the SXSSFWorkbook(XSSFWorkbook)constructor:

您可能有一个要向其中添加大量数据的模板。您需要使用SXSSFWorkbook(XSSFWorkbook)构造函数:

XSSFWorkbook wb = new XSSFWorkbook(new File("template.xlsx"));
SXSSFWorkbook wbss = new SXSSFWorkbook(wb, 100);
Sheet sheet = wbss.createSheet("sheet1");
// now add rows to sheet