//主要用于原来的excel模板已经存在合并区域、再次合并会导致合并异常
private boolean removeMergedRegion(Sheet sheet, CellRangeAddress mergedRegionToRemove) { boolean removed = false; for (int i = 0; i < sheet.getNumMergedRegions(); i++) { CellRangeAddress mergedRegion = sheet.getMergedRegion(i); if (mergedRegionToRemove.equals(mergedRegion)) { sheet.removeMergedRegion(i); removed = true; break; } } return removed; }
经过测试可以使用,样例:
Sheet sheet = workbook.getSheetAt(sheetIndex); removeMergedRegion(sheet,new CellRangeAddress(0,0,0,16));