From 24d2ca0fffebe005d59122186184b092d7eb0fc4 Mon Sep 17 00:00:00 2001 From: chengliejian <284067032@qq.com> Date: Thu, 7 Aug 2025 15:44:18 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20CellMergeStrategy?= =?UTF-8?q?=20=E4=B8=AD=20@ExcelIgnore=20=E5=AF=BC=E8=87=B4=E5=88=97?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E9=94=99=E4=BD=8D=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?,=E4=BB=A5=E5=8F=8A=E4=BF=AE=E5=A4=8D=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=9C=AA=E5=8A=A0@ExcelProperty=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=AF=BC=E8=87=B4=E7=A9=BA=E6=8C=87=E9=92=88=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/core/CellMergeStrategy.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 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 515f68e1b..af8c8d95e 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,7 @@ 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 cn.idev.excel.annotation.ExcelIgnore; import cn.idev.excel.annotation.ExcelProperty; import cn.idev.excel.metadata.Head; import cn.idev.excel.write.handler.WorkbookWriteHandler; @@ -75,25 +76,38 @@ public class CellMergeStrategy extends AbstractMergeStrategy implements Workbook // 有注解的字段 List mergeFields = new ArrayList<>(); List mergeFieldsIndex = new ArrayList<>(); - for (int i = 0; i < fields.length; i++) { - Field field = fields[i]; + // 实际 Excel 列索引(忽略被 @ExcelIgnore 的字段) + int excelColIndex = 0; + for (Field field : fields) { + if (field.isAnnotationPresent(ExcelIgnore.class)) { + // 忽略不导出的字段 + continue; + } if (field.isAnnotationPresent(CellMerge.class)) { CellMerge cm = field.getAnnotation(CellMerge.class); mergeFields.add(field); - mergeFieldsIndex.add(cm.index() == -1 ? i : cm.index()); + // 若 CellMerge.index() 被设置了,使用它,否则用当前实际 Excel 列索引 + mergeFieldsIndex.add(cm.index() == -1 ? excelColIndex : cm.index()); if (hasTitle) { ExcelProperty property = field.getAnnotation(ExcelProperty.class); - rowIndex = Math.max(rowIndex, property.value().length); + if (property != null && property.value().length > 0) { + rowIndex = Math.max(rowIndex, property.value().length); + } } } + + // 每处理一个未忽略的字段,实际 Excel 列索引 +1 + excelColIndex++; } Map map = new HashMap<>(); // 生成两两合并单元格 for (int i = 0; i < list.size(); i++) { + Object rowObj = list.get(i); + for (int j = 0; j < mergeFields.size(); j++) { Field field = mergeFields.get(j); - Object val = ReflectUtils.invokeGetter(list.get(i), field.getName()); + Object val = ReflectUtils.invokeGetter(rowObj, field.getName()); int colNum = mergeFieldsIndex.get(j); if (!map.containsKey(field)) { -- Gitee