这个问题是由于合并单元格不可以是相同的列
CellRangeAddress callRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);//起始行,结束行,起始列,结束列
sheet.addMergedRegion(callRangeAddress);
经排查,当firstCol = lastCol的时候 就会报错Merged region A1 must contain 2 or more cells,之前是poi3.12是可以的,升级之后不行了
查看源码:
public CellRangeAddress(int firstRow, int lastRow, int firstCol, int lastCol) {
super(firstRow, lastRow, firstCol, lastCol);
if (lastRow < firstRow || lastCol < firstCol) {
throw new IllegalArgumentException("Invalid cell range, having lastRow < firstRow || lastCol < firstCol, had rows " + lastRow + " >= " + firstRow + " or cells " + lastCol + " >= " + firstCol);
}
}
处理方式:
当firstCol = lastCol 或者 lastRow = firstRow的时候 不进行合并处理,我这样处理完就好用了
CellRangeAddress callRangeAddress = new CellRangeAddress(firstRow, lastRow,firstCol, lastCol);//起始行,结束行,起始列,结束列
if (firstCol<lastCol) {
sheet.addMergedRegionUnsafe(callRangeAddress);
}