Apache POI 需要花费大量时间来写入工作簿 [英] Apache POI is taking extremely large time to write into the Workbook

查看:40
本文介绍了Apache POI 需要花费大量时间来写入工作簿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Apache POI 创建记录并将其保存到工作簿.我有近 5000 多条新记录要写入并保存到工作簿中.但是在将fileOutputStream写入工作簿的时候,执行基本停止并变慢了.

I'm using Apache POI to create and save records into Workbook. I have almost 5000+ new records to be written and saved into the workbook. But at the time of writing the fileOutputStream into the workbook, the execution basically halts and slowed down.

我想说的是,在执行这一行的时候:

What I mean to say is, at the time of executing this line:

workbook.write(fileOutputStream);

它几乎停止处理 5000 多条记录.我确认在工作簿中书写需要将近 1 小时 (!).

it almost stops to process 5000+ records. I validated that it's taking nearly 1 hour (!) to write in the workbook.

我怎样才能提高性能并克服这个缺点??请建议...

How can I improve the performance and overcome this drawback?? Please suggest...

** 注意:其余的代码是正常的Apache POI相关代码,它们运行良好,没有问题,因此我没有提到所有这些.只有我卡在上面那行.

** Note: The rest of the codes are normal Apache POI related codes and they are executing fine, no issue, hence I didnot mention all of them. Only I got stuck at the above line.

我在这里找到了一个讨论:FileOutputStream (Apachhe POI) 保存时间太长

I found one discussion here: FileOutputStream (Apachhe POI) taking too long time to save

但是,它没有帮助我.我需要保存整个文件.

but, it did not help me. I need to save the whole file.

推荐答案

我理解的另一种解决方案,例如,在迭代 Row 并创建单元格时,不要继续声明 CellStylesheet.autoSizeColumn(colNumber) 在循环内部,而不是在循环外部仅声明一次这两个,并仅在循环内部设置值和样式,即 cell.setCellStylecell.setCellValue.

One more solution I understand, like, while iterating over the Row and creating cells, DO NOT keep declaring CellStyle and sheet.autoSizeColumn(colNumber) inside the loop, rather declare these 2 only once at the outside of the loop and set the values and style only inside the loop, i.e, cell.setCellStyle and cell.setCellValue.

每次迭代时都声明上述 2 项,基本上会从根本上降低 POI 的性能.

Declaring the above 2 everytime while iterating, basically degrades the performance of the POI radically.

这篇关于Apache POI 需要花费大量时间来写入工作簿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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