From 76a3663f3af301b33d342ee89c21594c79fb0481 Mon Sep 17 00:00:00 2001 From: songshuang Date: Tue, 16 Jan 2024 17:50:21 +0800 Subject: [PATCH] =?UTF-8?q?excel=E6=8F=92=E4=BB=B6=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E7=AD=96=E7=95=A5=20=E5=9C=A8=E5=90=88?= =?UTF-8?q?=E9=80=82=E7=9A=84=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E5=90=88=E5=B9=B6=20=E5=8E=BB=E9=99=A4=E8=A2=AB?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E5=8D=95=E5=85=83=E6=A0=BC=E7=9A=84=E9=9D=9E?= =?UTF-8?q?=E9=A6=96=E8=A1=8C=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/core/CellMergeStrategy.java | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java index bcb5be767..7dde91523 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java @@ -3,6 +3,8 @@ package org.dromara.common.excel.core; import cn.hutool.core.collection.CollUtil; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.Head; +import com.alibaba.excel.write.handler.WorkbookWriteHandler; +import com.alibaba.excel.write.handler.context.WorkbookWriteHandlerContext; import com.alibaba.excel.write.merge.AbstractMergeStrategy; import lombok.AllArgsConstructor; import lombok.Data; @@ -26,7 +28,7 @@ import java.util.Map; * @author Lion Li */ @Slf4j -public class CellMergeStrategy extends AbstractMergeStrategy { +public class CellMergeStrategy extends AbstractMergeStrategy implements WorkbookWriteHandler { private final List cellList; private final boolean hasTitle; @@ -41,17 +43,24 @@ public class CellMergeStrategy extends AbstractMergeStrategy { @Override protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) { - // judge the list is not null - if (CollUtil.isNotEmpty(cellList)) { - // the judge is necessary - if (cell.getRowIndex() == rowIndex && cell.getColumnIndex() == 0) { - for (CellRangeAddress item : cellList) { - sheet.addMergedRegion(item); - } + //单元格写入了,遍历合并区域,如果该Cell在区域内,但非首行,则清空 + final int rowIndex = cell.getRowIndex(); + for (CellRangeAddress cellAddresses : cellList) { + final int firstRow = cellAddresses.getFirstRow(); + if (cellAddresses.isInRange(cell) && rowIndex != firstRow){ + cell.setBlank(); } } } + @Override + public void afterWorkbookDispose(final WorkbookWriteHandlerContext context) { + //当前表格写完后,统一写入 + for (CellRangeAddress item : cellList) { + context.getWriteContext().writeSheetHolder().getSheet().addMergedRegion(item); + } + } + @SneakyThrows private List handle(List list, boolean hasTitle) { List cellList = new ArrayList<>();