int currentRowIndex = cell.getRowIndex();
if (!lastRow.containsKey(currentCellIndex)) {
// 记录首行起始位置
lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
return;
}
//有上行这列的值了,拿来对比.
MergeRange mergeRange = lastRow.get(currentCellIndex);
if (!(mergeRange.lastValue != null && mergeRange.lastValue.equals(currentCellValue))) {
// 结束的位置触发下合并.
// 同行同列不能合并,会抛异常
if (mergeRange.startRow != mergeRange.endRow || mergeRange.startCell != mergeRange.endCell) {
sheet.addMergedRegionUnsafe(new CellRangeAddress(mergeRange.startRow, mergeRange.endRow, mergeRange.startCell, mergeRange.endCell));
}
// 更新当前列起始位置
lastRow.put(currentCellIndex, new MergeRange(currentCellValue, currentRowIndex, currentRowIndex, currentCellIndex, currentCellIndex));
}
// 合并行 + 1
mergeRange.endRow += 1;
// 结束的位置触发下最后一次没完成的合并
if (relativeRowIndex.equals(maxRow - 1)) {
MergeRange lastMergeRange = lastRow.get(currentCellIndex);
// 同行同列不能合并,会抛异常
if (lastMergeRange.startRow != lastMergeRange.endRow || lastMergeRange.startCell != lastMergeRange.endCell) {
sheet.addMergedRegionUnsafe(new CellRangeAddress(lastMergeRange.startRow, lastMergeRange.endRow, lastMergeRange.startCell, lastMergeRange.endCell));
}
}
}
}
}
class MergeRange {
public int startRow;
public int endRow;
public int startCell;
public int endCell;
public String lastValue;
public MergeRange(String lastValue, int startRow, int endRow, int startCell, int endCell) {
this.startRow = startRow;
this.endRow = endRow;
this.startCell = startCell;
this.endCell = endCell;
this.lastValue = https://www.isolves.com/it/rj/jy/2023-03-09/lastValue;
}
}
作者:菜C++鸡java https://blog.csdn.NET/qq_40921561/article/details/126764038
【实测好用 EasyExcel带格式多线程导出百万数据】
推荐阅读
- 海运dhc是什么费用……dhc公铁好用吗?
- 免洗发膜哪个牌子好用 什么发膜好用
- 竞价点击器哪个好用。请问百度竞价点击器在哪里能找到
- 最好用的电脑手写板 手写板软件
- 鱼子酱发膜真的有用吗?十大最好用发膜排行榜?
- 好用的润唇膏 小蜜缇润唇膏
- 露华浓洗发水好用吗 露华浓生姜
- 花印怎么样好用吗 花印怎么样
- BT搜索蜘蛛 bt磁力搜索
- 娇韵诗哪个水好用 娇韵诗防晒霜
