问题描述
我使用以下代码来自动调整我的电子表格中的列:
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行的大型电子表格。对于小文件来说,它变得非常快。有什么可以帮助自动调整工作更快吗?
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自动调整列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!