支持 @ExcelIgnoreUnannotated 注解,修复未注解字段导致列合并错位的问题

This commit is contained in:
chengliejian 2025-08-07 16:10:48 +08:00
parent 24d2ca0fff
commit d401642201

View File

@ -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<Field> mergeFields = new ArrayList<>();
List<Integer> 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);
}
}