It's currently possible in POI to create a spreadsheet with overlapping merged regions. When saved to a file, this results in a corrupted workbook that Microsoft Excel must repair before being able to read. In order to eliminate the possibility of creating a corrupted workbook from merged regions, Sheet.addMergedRegion(CellRangeAddress) should check existing merged regions for any existing regions that overlap with the candidate.
Created attachment 33128 [details] patch to check for intersection when adding a merged region Fixes: * remove HSSFSheet.addMergedRegion(Region), which has been deprecated since Aug 2008 * modified H/XSSFSheet.addMergedRegion to check if candidate region overlaps with an existing region, and throw an IllegalStateException if there is overlap, and corresponding unit test * add CellRangeAddressBase.intersects and corresponding unit test * fixed several unit tests that unintentionally created overlapping merged regions. Note: this patch requires the fix for bug 58350 (attachment 33086 [details]).
Created attachment 33129 [details] patch to check for intersection when adding a merged region fixed whitespace
Current implementation runs in linear time to add one merged region, and quadratic time if adding multiple regions. Is there a faster way (O(n log n)?) to add multiple regions? If so, we may want to add a addMergedRegions(List<CellRangeAddress>) if users plan on adding many merged regions.
Applied to trunk in r1711581 and r1711586: * prevent corrupted workbooks by checking for overlapping merged regions before adding a merged region to a sheet * fix unit tests that produced corrupt workbooks due to overlapping merged regions * remove deprecated HSSFSheet#addMergedRegion(Region) Documentation updated in r1711590.
*** Bug 58070 has been marked as a duplicate of this bug. ***
Spun performance regression off as bug 58885