Search code examples
apache-poisxssf

Apache POI Difference between SXSSFSheet.addMergedRegion() & SXSSFSheet.addMergedRegionUnsafe()?


I am using Apache POI to build excel file. Since the data might be very large I decided to use SXSSFWorkbook instead of XSSFWorkbook. In application runtime I can see that SXSSFSheet.addMergedRegionUnsafe() run faster than SXSSFSheet.addMergedRegion() but I can't find any docs related to it. So I wonder is there any risk of using SXSSFSheet.addMergedRegionUnsafe()?


Solution

  • SXSSFSheet.addMergedRegionUnsafe simply calls XSSFSheet.addMergedRegionUnsafe. See source code of SXSSFSheet:

    ...    
    @Override
    public int addMergedRegionUnsafe(CellRangeAddress region) {
     return _sh.addMergedRegionUnsafe(region);
    }
    ...
    

    There _sh is XSSFSheet.

    And in XSSFSheet.addMergedRegionUnsafe the behavior is documented:

    Adds a merged region of cells (hence those cells form one). Skips validation. It is possible to create overlapping merged regions or create a merged region that intersects a multi-cell array formula with this formula, which may result in a corrupt workbook. To check for merged regions overlapping array formulas or other merged regions after addMergedRegionUnsafe has been called, call validateMergedRegions(), which runs in O(n^2) time.

    So addMergedRegionUnsafe runs faster because it skips validation. So using this your program needs considering not to create overlapping merged regions and not to create a merged region that intersects a multi-cell array formula with this formula. Else the result workbook gets corrupted.

    Source for XSSFSheet.addMergedRegionUnsafe for completeness.