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); } }