如何使用 SXSSF 写入现有文件? [英] How to write to an existing file using SXSSF?

查看:36
本文介绍了如何使用 SXSSF 写入现有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

此外,我不允许加载"现有文件,即

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); 

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

I get the error that type conversion is not allowed from XSSFSheet to 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?

推荐答案

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

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

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

这篇关于如何使用 SXSSF 写入现有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆