这个问题是由于合并单元格不可以是相同的列

	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);
	}
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐