From 70bf1a48d037f993021ddf4ee2ee6da3bbce351e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B8=E7=8C=AB=E5=AD=90?= Date: Sun, 4 Feb 2024 01:51:52 +0000 Subject: [PATCH] =?UTF-8?q?!481=20=E5=8D=95=E5=85=83=E6=A0=BC=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=8F=AF=E4=BB=A5=E5=9F=BA=E4=BA=8E=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E9=80=89=E6=8B=A9=E5=93=AA=E4=BA=9B=E5=85=B6=E4=BB=96=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=E9=9C=80=E8=A6=81=E5=90=8C=E6=97=B6=E7=9B=B8=E7=AD=89?= =?UTF-8?q?=E6=96=B9=E5=8F=AF=E8=BF=9B=E8=A1=8C=E8=AF=A5=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=A0=BC=E5=90=88=E5=B9=B6=20*=20excel=E6=8F=92=E4=BB=B6=20?= =?UTF-8?q?=E5=8E=9F=E6=9C=89=E7=9A=84=E5=90=88=E5=B9=B6=E7=AD=96=E7=95=A5?= =?UTF-8?q?=E5=86=99=E6=AD=BB=E4=BA=86=E6=A0=B9=E6=8D=AE=E5=89=8D=E4=B8=80?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=98=AF=E5=90=A6=E4=B8=80=E8=87=B4=E6=9D=A5?= =?UTF-8?q?=E5=88=A4=E6=96=AD=E5=90=8E=E4=B8=80=E5=AD=97=E6=AE=B5=E7=9A=84?= =?UTF-8?q?=E5=90=88=E5=B9=B6=20=E6=84=9F=E8=A7=89=E8=BF=99=E6=A0=B7?= =?UTF-8?q?=E4=B8=8D=E7=81=B5=E6=B4=BB=20=E5=A6=82=E6=9C=89=E7=89=B9?= =?UTF-8?q?=E6=AE=8A=E9=9C=80=E6=B1=82=20=E5=AD=97=E6=AE=B5=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E4=B8=8D=E8=A7=84=E6=95=B4=20=E4=BA=8E=E6=98=AF?= =?UTF-8?q?=E6=89=A9=E5=85=85=E4=BA=86=E6=B3=A8=E8=A7=A3=E7=9A=84=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/annotation/CellMerge.java | 5 ++ .../common/excel/core/CellMergeStrategy.java | 55 +++++++++---------- 2 files changed, 31 insertions(+), 29 deletions(-) diff --git a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/CellMerge.java b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/CellMerge.java index bbdaaa14d..6b9211b53 100644 --- a/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/CellMerge.java +++ b/ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/annotation/CellMerge.java @@ -21,4 +21,9 @@ public @interface CellMerge { */ int index() default -1; + /** + * 合并需要依赖的其他字段名称 + */ + String[] mergeBy() default {}; + } 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..6388a9dcd 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 @@ -1,6 +1,8 @@ package org.dromara.common.excel.core; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.ReflectUtil; +import cn.hutool.core.util.StrUtil; import com.alibaba.excel.annotation.ExcelProperty; import com.alibaba.excel.metadata.Head; import com.alibaba.excel.write.merge.AbstractMergeStrategy; @@ -15,10 +17,7 @@ import org.dromara.common.core.utils.reflect.ReflectUtils; import org.dromara.common.excel.annotation.CellMerge; import java.lang.reflect.Field; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * 列值重复合并策略 @@ -93,35 +92,15 @@ public class CellMergeStrategy extends AbstractMergeStrategy { // 空值跳过不合并 continue; } + if (!cellValue.equals(val)) { - if (i - repeatCell.getCurrent() > 1) { + if ((i - repeatCell.getCurrent() > 1) && isMerge(list, i, field)) { cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); } map.put(field, new RepeatCell(val, i)); - } else if (j == 0) { - if (i == list.size() - 1) { - if (i > repeatCell.getCurrent()) { - cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); - } - } - } else { - // 判断前面的是否合并了 - RepeatCell firstCell = map.get(mergeFields.get(0)); - if (repeatCell.getCurrent() != firstCell.getCurrent()) { - if (i == list.size() - 1) { - if (i > repeatCell.getCurrent()) { - cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); - } - } else if (repeatCell.getCurrent() < firstCell.getCurrent()) { - if (i - repeatCell.getCurrent() > 1) { - cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex - 1, colNum, colNum)); - } - map.put(field, new RepeatCell(val, i)); - } - } else if (i == list.size() - 1) { - if (i > repeatCell.getCurrent()) { - cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); - } + } else if (i == list.size() - 1) { + if (i > repeatCell.getCurrent() && isMerge(list, i, field)) { + cellList.add(new CellRangeAddress(repeatCell.getCurrent() + rowIndex, i + rowIndex, colNum, colNum)); } } } @@ -130,6 +109,24 @@ public class CellMergeStrategy extends AbstractMergeStrategy { return cellList; } + private boolean isMerge(List list, int i, Field field) { + boolean isMerge = true; + CellMerge cm = field.getAnnotation(CellMerge.class); + final String[] mergeBy = cm.mergeBy(); + if (StrUtil.isAllNotBlank(mergeBy)) { + //比对当前list(i)和list(i - 1)的各个属性值一一比对 如果全为真 则为真 + for (String fieldName : mergeBy) { + final Object valCurrent = ReflectUtil.getFieldValue(list.get(i), fieldName); + final Object valPre = ReflectUtil.getFieldValue(list.get(i - 1), fieldName); + if (!Objects.equals(valPre, valCurrent)) { + //依赖字段如有任一不等值,则标记为不可合并 + isMerge = false; + } + } + } + return isMerge; + } + @Data @AllArgsConstructor static class RepeatCell {