Excel导入导出支持注解写到构造上
This commit is contained in:
@@ -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){
|
||||
|
||||
@@ -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){
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user