问题描述
我使用以下代码来自动调整电子表格中的列大小:
I use the following code in order to autosize columns in my spreadsheet:
for (int i = 0; i < columns.size(); i++) {
sheet.autoSizeColumn(i, true);
sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 600);
}
问题是在超过 3000 行的大型电子表格的情况下,自动调整每列需要 10 多分钟.但是对于小文档,它运行得非常快.有什么可以帮助自动调整以更快地工作吗?
The problem is it takes more than 10 minutes to autosize each column in case of large spreadsheets with more than 3000 rows. It goes very fast for small documents though. Is there anything which could help autosizing to work faster?
推荐答案
对我有用的解决方案:
Solution which worked for me:
可以避免合并区域,因此我可以遍历其他单元格并最终自动调整到最大的单元格,如下所示:
It was possible to avoid merged regions, so I could iterate through the other cells and finally autosize to the largest cell like this:
int width = ((int)(maxNumCharacters * 1.14388)) * 256;
sheet.setColumnWidth(i, width);
其中 1.14388 是Serif"字体和 256 个字体单位的最大字符宽度.
where 1.14388 is a max character width of the "Serif" font and 256 font units.
自动调整大小的性能从 10 分钟提高到 6 秒.
Performance of autosizing improved from 10 minutes to 6 seconds.
这篇关于如何加快apache POI中自动调整列的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!