Excel导入导出支持注解写到构造上

This commit is contained in:
thinkgem
2021-10-22 09:50:26 +08:00
parent 9aa8c2d4df
commit a839cb37b1
4 changed files with 24 additions and 2 deletions

View File

@@ -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){

View File

@@ -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 <E> List<E> getDataList(Class<E> cls, MethodCallback exceptionCallback, String... groups) throws Exception{
List<Object[]> 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){

View File

@@ -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 {

View File

@@ -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 {