poi 3.17合并单元格报错 java.lang.IllegalArgumentException: Merged region A1 must contain 2 or more cells

本文讲述了在使用poi库升级后,合并单元格时遇到的错误,即不能在同一列中进行合并。作者提供了处理方式,即排除firstCol等于lastCol的情况,并给出了代码示例。

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

	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);
	}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值