From a839cb37b1a7b1a097dd476d9900945aa849e725 Mon Sep 17 00:00:00 2001 From: thinkgem Date: Fri, 22 Oct 2021 09:50:26 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E5=AF=BC=E5=85=A5=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=B3=A8=E8=A7=A3=E5=86=99=E5=88=B0=E6=9E=84?= =?UTF-8?q?=E9=80=A0=E4=B8=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/jeesite/common/utils/excel/ExcelExport.java | 11 +++++++++++ .../com/jeesite/common/utils/excel/ExcelImport.java | 11 +++++++++++ .../common/utils/excel/annotation/ExcelField.java | 2 +- .../common/utils/excel/annotation/ExcelFields.java | 2 +- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java index ff9ed47c..9c20dba4 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelExport.java @@ -8,6 +8,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -202,6 +203,16 @@ public class ExcelExport implements Closeable{ */ public void createSheet(String sheetName, String title, Class cls, Type type, String... groups){ this.annotationList = ListUtils.newArrayList(); + // Get constructor annotation + Constructor[] cs = cls.getConstructors(); + for (Constructor c : cs) { + ExcelFields efs = c.getAnnotation(ExcelFields.class); + if (efs != null && efs.value() != null){ + for (ExcelField ef : efs.value()){ + addAnnotation(annotationList, ef, c, type, groups); + } + } + } // Get annotation field Field[] fs = cls.getDeclaredFields(); for (Field f : fs){ diff --git a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java index 69dc42e1..420afa3e 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/ExcelImport.java @@ -8,6 +8,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -354,6 +355,16 @@ public class ExcelImport implements Closeable { @SuppressWarnings("unchecked") public List getDataList(Class cls, MethodCallback exceptionCallback, String... groups) throws Exception{ List annotationList = ListUtils.newArrayList(); + // Get constructor annotation + Constructor[] cs = cls.getConstructors(); + for (Constructor c : cs) { + ExcelFields efs = c.getAnnotation(ExcelFields.class); + if (efs != null && efs.value() != null){ + for (ExcelField ef : efs.value()){ + addAnnotation(annotationList, ef, c, Type.IMPORT, groups); + } + } + } // Get annotation field Field[] fs = cls.getDeclaredFields(); for (Field f : fs){ diff --git a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java index 4e13cdd9..d003aade 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelField.java @@ -15,7 +15,7 @@ import com.jeesite.common.utils.excel.fieldtype.FieldType; * @author ThinkGem * @version 2020-3-5 */ -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE}) +@Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelField { diff --git a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelFields.java b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelFields.java index 4dfedd10..2af861b9 100644 --- a/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelFields.java +++ b/common/src/main/java/com/jeesite/common/utils/excel/annotation/ExcelFields.java @@ -13,7 +13,7 @@ import java.lang.annotation.Target; * @author ThinkGem * @version 2016-11-9 */ -@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE}) +@Target({ElementType.METHOD, ElementType.FIELD, ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) public @interface ExcelFields {