3

I try to apply autoSizeColumn on an Excel sheet. I'm using POI 3.10.1.
I apply autoSizeColumn at the end but the problem is than the process is too slow/long anyway: On the sheet I've approximately 1000 lines and 20 columns... After 5 hours, I kill the process ...
I don't understand what is taking so long, 1000 lines and 20 columns doesn't appear so huge: Did I miss something? (nb: on a smaller file, it's working)
My simplified code below:

    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
    }

I've already read threads below :

    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

But none solve my issue.
Any idea ? PS : I tried to upload an example image of the Excel file but I don't find how to upload it.

6w3n
  • 31
  • 4

3 Answers3

0

Even after using Apache POI 3.12, I was facing the same issue for auto-sizing. Also auto-size doesn't work in Unix/Linux. What I learnt from various forums is this: 1.You can try is using the SXSSF API, usually works much faster. 2. If not, then go for setColumnWidth method(I know its literally manual work for 20 columns)

sudipta06
  • 658
  • 7
  • 12
0

My solution was only use Autosize on the last Excel line, like this:

if (i >= abaExcel.Itens.Count)
    sheet.AutoSizeColumn(j);
Taian
  • 189
  • 8
  • 19
0

Because merged regions cannot overlap without producing a corrupt document, POI may be checking the list of merged regions on a sheet for potential intersections before adding a merged region. This gives O(N) behavior for adding one region instead of the expected O(1).

  1. addMergedRegion - with checking but slow

  2. addMergedRegionUnsafe - without checking but fast

Documentation: read more about addMergedRegionUnsafe(...)

Andrew Samsonov
  • 30
  • 1
  • 1
  • 8