From 70bf1a48d037f993021ddf4ee2ee6da3bbce351e Mon Sep 17 00:00:00 2001
From: 司猫子 <songshuang@qq.com>
Date: 星期日, 04 二月 2024 09:51:52 +0800
Subject: [PATCH] !481 单元格合并可以基于注解选择哪些其他字段需要同时相等方可进行该单元格合并 * excel插件 原有的合并策略写死了根据前一字段是否一致来判断后一字段的合并 感觉这样不灵活 如有特殊需求 字段排序不规整 于是扩充了注解的…

---
 ruoyi-common/ruoyi-common-excel/src/main/java/org/dromara/common/excel/core/CellMergeStrategy.java |   55 ++++++++++++++++++++++++++-----------------------------
 1 files changed, 26 insertions(+), 29 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 bcb5be7..6388a9d 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.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 @@
                         // 绌哄�艰烦杩囦笉鍚堝苟
                         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 @@
         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)鍜宭ist(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 {

--
Gitblit v1.9.3