From d40164220119ac2ad59f5ba091c4178389293e92 Mon Sep 17 00:00:00 2001 From: chengliejian <284067032@qq.com> Date: Thu, 7 Aug 2025 16:10:48 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20@ExcelIgnoreUnannotated=20?= =?UTF-8?q?=E6=B3=A8=E8=A7=A3=EF=BC=8C=E4=BF=AE=E5=A4=8D=E6=9C=AA=E6=B3=A8?= =?UTF-8?q?=E8=A7=A3=E5=AD=97=E6=AE=B5=E5=AF=BC=E8=87=B4=E5=88=97=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E9=94=99=E4=BD=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/excel/core/CellMergeStrategy.java | 14 +++++++++----- 1 file changed, 9 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 af8c8d95e..6e547ebb8 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 @@ -4,6 +4,7 @@ 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.ExcelIgnoreUnannotated; import cn.idev.excel.annotation.ExcelProperty; import cn.idev.excel.metadata.Head; import cn.idev.excel.write.handler.WorkbookWriteHandler; @@ -71,8 +72,9 @@ public class CellMergeStrategy extends AbstractMergeStrategy implements Workbook if (CollUtil.isEmpty(list)) { return cellList; } - Field[] fields = ReflectUtils.getFields(list.get(0).getClass(), field -> !"serialVersionUID".equals(field.getName())); - + Class clazz = list.get(0).getClass(); + Field[] fields = ReflectUtils.getFields(clazz, field -> !"serialVersionUID".equals(field.getName())); + boolean ignoreUnannotated = clazz.isAnnotationPresent(ExcelIgnoreUnannotated.class); // 有注解的字段 List mergeFields = new ArrayList<>(); List mergeFieldsIndex = new ArrayList<>(); @@ -83,6 +85,10 @@ public class CellMergeStrategy extends AbstractMergeStrategy implements Workbook // 忽略不导出的字段 continue; } + if (ignoreUnannotated && !field.isAnnotationPresent(ExcelProperty.class)) { + // 忽略未注解字段(根据 @ExcelIgnoreUnannotated) + continue; + } if (field.isAnnotationPresent(CellMerge.class)) { CellMerge cm = field.getAnnotation(CellMerge.class); mergeFields.add(field); @@ -90,9 +96,7 @@ public class CellMergeStrategy extends AbstractMergeStrategy implements Workbook mergeFieldsIndex.add(cm.index() == -1 ? excelColIndex : cm.index()); if (hasTitle) { ExcelProperty property = field.getAnnotation(ExcelProperty.class); - if (property != null && property.value().length > 0) { - rowIndex = Math.max(rowIndex, property.value().length); - } + rowIndex = Math.max(rowIndex, property.value().length); } }