带有useMergedCells的APACHE POI autoSizeColumn太慢

时间:2015-05-21 12:48:15

标签: java excel performance apache-poi

我尝试在Excel工作表上应用autoSizeColumn。我正在使用POI 3.10.1 我在最后应用了autoSizeColumn,但问题是过程太慢/太长了: 在表格上我大约有1000行和20列...... 5个小时后,我终止了这个过程...
我不明白花了这么长时间,1000行和20列看起来不那么大:我错过了什么吗? (nb:在较小的文件上,它正在工作)
我的简化代码如下:

    Workbook vWorkbook = getWorkbook();

    Sheet vSheet = vWorkbook.createSheet("sheet");
    CreationHelper vHelper = vWorkbook.getCreationHelper();
    Drawing drawing = vSheet.createDrawingPatriarch();

    Set<CellRangeAddress> vRegions = new HashSet<CellRangeAddress>();

    //Parcours des lignes du document
    MatrixDocument vMatrixDocument = getMatrixDocument();
    List<MatrixRow> vListMatrixRows = vMatrixDocument.getRows();
    int maxColNb = 0;

    //Parcours des lignes de la grille.
    for (MatrixRow vMatrixRow : vListMatrixRows)
    {
        //(...)
        //create cells 
        //(...)
    }

    initColSpan(vListMatrixRows, vRegions);

    //Gestion des colSpan et des RowSpan
    for (CellRangeAddress vRegion : vRegions)
    {
        vSheet.addMergedRegion(vRegion);
    }

    for (int i = 0; i < maxColNb; ++i)
    {
        vSheet.autoSizeColumn(i, true);//Here the problem. spent more than 5 hour for 1000 lines and 20 columns
    }

我已阅读下面的主题:

    http://stackoverflow.com/questions/16943493/apache-poi-autosizecolumn-resizes-incorrectly
    http://stackoverflow.com/questions/15740100/apache-poi-autosizecolumn-not-working-right
    http://stackoverflow.com/questions/23366606/autosizecolumn-performance-effect-in-apache-poi
    http://stackoverflow.com/questions/18984785/a-poi-related-code-block-running-dead-slow
    http://stackoverflow.com/questions/28564045/apache-poi-autosizecolumn-behaving-weird
    http://stackoverflow.com/questions/18456474/apache-poi-autosizecolumn-is-not-working

但没有人解决我的问题 任何的想法 ? PS:我试图上传Excel文件的示例图片,但我找不到如何上传它。

2 个答案:

答案 0 :(得分:0)

即使在使用Apache POI 3.12之后,我也面临着自动调整大小的问题。自动大小在Unix / Linux中也不起作用。 我从各种论坛中学到的是: 1.您可以尝试使用SXSSF API,通常工作得更快。 2.如果没有,那么去setColumnWidth方法(我知道它的字面意思是20列的手工工作)

答案 1 :(得分:0)

我的解决方案是仅在Excel的最后一行使用“自动调整大小”,例如:

if (i >= abaExcel.Itens.Count)
    sheet.AutoSizeColumn(j);