8
触发场景描述 按照excel模板导出xlsx文件,数据列表需要动态填充,动态填充的行数据需要合并单元格,测试发现动态填充7行没有问题,填充8行以上时就报错,如:Cannot add merged region C25:D25 to sheet because it overlaps with an existing merged region (B25:L25). 模板文件格式类似是这样的:
触发Bug的代码
//单元格合并
List<CellRangeAddress> cellRangeAddress = new ArrayList<>();
if (CollectionUtil.isNotEmpty(excelVoList)) {
if (excelVoList.size() > 1) {
for (int i = 0; i < excelVoList.size(); i++) {
cellRangeAddress.add(new CellRangeAddress(firstRow, lastRow, 2, 3));
cellRangeAddress.add(new CellRangeAddress(firstRow, lastRow, 7, 9));
cellRangeAddress.add(new CellRangeAddress(firstRow, lastRow, 12, 13));
firstRow++;
lastRow++;
}
}
}
......
ExcelWriter excelWriter = EasyExcel.write(os).withTemplate(inputStream)
.registerWriteHandler(piFillMergeStrategy)
.build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
//参数集合,直接写入到Excel数据
excelWriter.fill(paramsMap, writeSheet);
//列表数据
excelWriter.fill(excelVoList, fillConfig, writeSheet);
单元格合并策略是这样的:
@Override
public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer relativeRowIndex, Boolean isHead) {
if (CollectionUtil.isNotEmpty(cellRangeAddress)) {
if (row.getRowNum() >= beginRow && row.getRowNum() <= beginRow + mergeRows) {
for (CellRangeAddress item : cellRangeAddress) {
writeSheetHolder.getSheet().addMergedRegionUnsafe(item);
}
}
}
}
提示的异常或者没有达到的效果 同样的逻辑,数据列表7行(包括)以下导出没有问题,7行以上导出就会报错