项目因为某些原因,把poi升级到了最新版本5.0.0。导致原有的程序合并单元格出错
java.lang.IllegalArgumentException: Merged region B4 must contain 2 or more cells:
原因:合并单元格不能再相同的行列.
修改为不是同行同列后,又出现了:
java.lang.IllegalStateException: Cannot add merged region B5:B6 to sheet because it overlaps with an existing merged region (B4:B5).
原因:不能合并上面已经合并过的单元格。
要解决这个问题就得找对集合中重复元素的正确其实合并位置;就在准备动手改代码时,发现源码有这样一个方法 addMergedRegionUnsafe:
文档解释: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.也就是跳过了很多安全检验,为了不修改大量源码,可以采用 addMergedRegionUnsafe()方法。