How to speed up autosizing columns in apache POI?

前端 未结 2 1943
温柔的废话
温柔的废话 2020-12-25 12:51

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


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-25 13:35

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

    where 1.14388 is a max character width of the "Serif" font and 256 font units.

    Performance of autosizing improved from 10 minutes to 6 seconds.

提交回复
热议问题