From 3dc6f7a762d8a803f4ca1150a85a7fb3c6936c21 Mon Sep 17 00:00:00 2001 From: TangDH03 <315617683@qq.com> Date: Fri, 18 Oct 2019 12:02:56 +0800 Subject: [PATCH] ignore static final and transient --- .../excel/metadata/property/ExcelHeadProperty.java | 9 +++++++++ .../easyexcel/test/demo/write/ComplexHeadData.java | 2 ++ .../com/alibaba/easyexcel/test/demo/write/WriteTest.java | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java index a1200d5..096d7d6 100644 --- a/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java +++ b/src/main/java/com/alibaba/excel/metadata/property/ExcelHeadProperty.java @@ -1,6 +1,7 @@ package com.alibaba.excel.metadata.property; import java.lang.reflect.Field; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -123,6 +124,14 @@ public class ExcelHeadProperty { Map customFiledMap = new TreeMap(); for (Field field : fieldList) { ExcelIgnore excelIgnore = field.getAnnotation(ExcelIgnore.class); + if(Modifier.isStatic(field.getModifiers())&&Modifier.isFinal(field.getModifiers())){ + ignoreMap.put(field.getName(),field); + continue; + } + if(Modifier.isTransient(field.getModifiers())){ + ignoreMap.put(field.getName(),field); + continue; + } if (excelIgnore != null) { ignoreMap.put(field.getName(), field); continue; diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java index 44ce5cc..4c5d0c6 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/ComplexHeadData.java @@ -13,6 +13,8 @@ import lombok.Data; **/ @Data public class ComplexHeadData { + private final static String TITLE = "title"; + private transient String trans = "transient"; @ExcelProperty({"主标题", "字符串标题"}) private String string; @ExcelProperty({"主标题", "日期标题"}) diff --git a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index 76c3c71..33420ed 100644 --- a/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -85,7 +85,7 @@ public class WriteTest { *

* 2. 使用{@link ExcelProperty}注解指定复杂的头 *

- * 3. 直接写即可 + * 3. 直接写即可,写入时会忽略static final和 transient */ @Test public void complexHeadWrite() {