Repository: wangguanquan/eec
Pull request #54 (2019-06-21 02:13:12)

Add annotation ExcelColumn to replace the annotation DisplayName


Patch:
diff --git a/README.md b/README.md
index 6cd35ce5..c8abfd3f 100755
--- a/README.md
+++ b/README.md
@@ -96,20 +96,20 @@ xls格式的读取与xlsx对外暴露完全一样，ExcelReader内部判断文
 #### 1. 对象数组导出
 
 #### 1.1 准备工作
-对象数组导出时可以在对象上使用注解`@DisplayName("column name")`来设置excel头部信息，
+对象数组导出时可以在对象上使用注解`@ExcelColumn("column name")`来设置excel头部信息，
 使用注解`@IgnoreExport`标记不需要导出的字段。
 
 ```
     @NotExport("敏感信息不导出")
     private int id; // not export
 
-    @DisplayName("渠道ID")
+    @ExcelColumn("渠道ID")
     private int channelId;
 
-    @DisplayName(share = false)
+    @ExcelColumn(share = false)
     private String account;
 
-    @DisplayName("注册时间")
+    @ExcelColumn("注册时间")
     private Timestamp registered;
 ```
 
diff --git a/src/main/java/org/ttzero/excel/annotation/DisplayName.java b/src/main/java/org/ttzero/excel/annotation/DisplayName.java
index d9590f29..c2337c01 100644
--- a/src/main/java/org/ttzero/excel/annotation/DisplayName.java
+++ b/src/main/java/org/ttzero/excel/annotation/DisplayName.java
@@ -27,12 +27,16 @@ import java.lang.annotation.Target;
 /**
  * Specify export information for exporting object-array
  * <p>
+ * The annotation name is awkward, please use the new annotation
+ * {@link ExcelColumn}
+ * <p>
  * Created by guanquan.wang at 2018-01-30 13:23
  */
 @Target({ElementType.FIELD})
 @Retention(RetentionPolicy.RUNTIME)
 @Inherited
 @Documented
+@Deprecated
 public @interface DisplayName {
     /**
      * header cell
diff --git a/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java b/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java
new file mode 100644
index 00000000..3d162b00
--- /dev/null
+++ b/src/main/java/org/ttzero/excel/annotation/ExcelColumn.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 2019, guanquan.wang@yandex.com All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.ttzero.excel.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Specify export information
+ * <p>
+ * Create by guanquan.wang at 2019-06-21 09:53
+ */
+@Target({ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+@Inherited
+@Documented
+public @interface ExcelColumn {
+    /**
+     * header cell
+     *
+     * @return the column name
+     */
+    String value() default "";
+
+    /**
+     * share body string
+     *
+     * @return true if shared
+     */
+    boolean share() default true;
+}
diff --git a/src/main/java/org/ttzero/excel/entity/ListSheet.java b/src/main/java/org/ttzero/excel/entity/ListSheet.java
index 0f952856..e4734696 100644
--- a/src/main/java/org/ttzero/excel/entity/ListSheet.java
+++ b/src/main/java/org/ttzero/excel/entity/ListSheet.java
@@ -16,6 +16,7 @@
 
 package org.ttzero.excel.entity;
 
+import org.ttzero.excel.annotation.ExcelColumn;
 import org.ttzero.excel.reader.Cell;
 import org.ttzero.excel.annotation.DisplayName;
 import org.ttzero.excel.annotation.IgnoreExport;
@@ -26,6 +27,8 @@ import java.lang.reflect.Field;
 import java.util.ArrayList;
 import java.util.List;
 
+import static org.ttzero.excel.util.StringUtil.isNotEmpty;
+
 /**
  * List is the most important data source, you can pass all
  * the data at a time, or customize the worksheet to extends
@@ -301,11 +304,11 @@ public class ListSheet<T> extends Sheet {
      * Get the first object of the object array witch is not NULL,
      * reflect all declared fields, and then do the following steps
      * <p>
-     * step 1. If there is has {@link DisplayName} annotation, the value of
+     * step 1. If there is has {@link ExcelColumn} annotation, the value of
      * this annotation is used as the column name.
      * <p>
-     * step 2. If the {@link DisplayName} annotation has no value or no
-     * {@link DisplayName} annotation, the field name is used as the column name.
+     * step 2. If the {@link ExcelColumn} annotation has no value or no
+     * {@link ExcelColumn} annotation, the field name is used as the column name.
      * <p>
      * step 3. Skip this Field if field has a {@link IgnoreExport} annotation
      * <p>
@@ -329,7 +332,11 @@ public class ListSheet<T> extends Sheet {
                     continue;
                 }
                 DisplayName dn = field.getAnnotation(DisplayName.class);
-                if (dn != null && StringUtil.isNotEmpty(dn.value())) {
+                ExcelColumn ec = field.getAnnotation(ExcelColumn.class);
+                if (ec != null && isNotEmpty(ec.value())) {
+                    list.add(new Column(ec.value(), field.getName()
+                        , field.getType()).setShare(ec.share()));
+                } else if (dn != null && isNotEmpty(dn.value())) {
                     list.add(new Column(dn.value(), field.getName()
                         , field.getType()).setShare(dn.share()));
                 } else {
diff --git a/src/main/java/org/ttzero/excel/reader/HeaderRow.java b/src/main/java/org/ttzero/excel/reader/HeaderRow.java
index 05150113..59064e80 100644
--- a/src/main/java/org/ttzero/excel/reader/HeaderRow.java
+++ b/src/main/java/org/ttzero/excel/reader/HeaderRow.java
@@ -17,6 +17,7 @@
 package org.ttzero.excel.reader;
 
 import org.ttzero.excel.annotation.DisplayName;
+import org.ttzero.excel.annotation.ExcelColumn;
 import org.ttzero.excel.annotation.IgnoreImport;
 import org.ttzero.excel.util.StringUtil;
 
@@ -24,6 +25,8 @@ import java.lang.reflect.Field;
 import java.util.Arrays;
 import java.util.StringJoiner;
 
+import static org.ttzero.excel.util.StringUtil.isNotEmpty;
+
 /**
  * Create by guanquan.wang at 2019-04-17 11:55
  */
@@ -84,7 +87,15 @@ class HeaderRow extends Row {
             }
             // field has display name
             DisplayName ano = f.getAnnotation(DisplayName.class);
-            if (ano != null && StringUtil.isNotEmpty(ano.value())) {
+            ExcelColumn ec = f.getAnnotation(ExcelColumn.class);
+            if (ec != null && isNotEmpty(ec.value())) {
+                n = StringUtil.indexOf(names, ec.value());
+                if (n == -1) {
+                    logger.warn(clazz + " field [" + ec.value() + "] can't find in header" + Arrays.toString(names));
+                    fields[i] = null;
+                    continue;
+                }
+            } else if (ano != null && isNotEmpty(ano.value())) {
                 n = StringUtil.indexOf(names, ano.value());
                 if (n == -1) {
                     logger.warn(clazz + " field [" + ano.value() + "] can't find in header" + Arrays.toString(names));


Test patch:
diff --git a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
index e96d9742..678ee5f4 100644
--- a/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
+++ b/src/test/java/org/ttzero/excel/entity/ListObjectSheetTest.java
@@ -18,7 +18,7 @@ package org.ttzero.excel.entity;
 
 import org.junit.Test;
 import org.ttzero.excel.Print;
-import org.ttzero.excel.annotation.DisplayName;
+import org.ttzero.excel.annotation.ExcelColumn;
 import org.ttzero.excel.annotation.IgnoreExport;
 import org.ttzero.excel.entity.style.Fill;
 import org.ttzero.excel.entity.style.PatternType;
@@ -383,9 +383,9 @@ public class ListObjectSheetTest extends WorkbookTest{
     public static class Student {
         @IgnoreExport
         private int id;
-        @DisplayName("姓名")
+        @ExcelColumn("姓名")
         private String name;
-        @DisplayName("成绩")
+        @ExcelColumn("成绩")
         private int score;
 
         Student(int id, String name, int score) {