|
@@ -9,6 +9,7 @@ import com.sckw.core.common.enums.NumberConstant;
|
|
|
import com.sckw.excel.annotation.EasyExcel;
|
|
import com.sckw.excel.annotation.EasyExcel;
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Field;
|
|
|
|
|
+import java.lang.reflect.InvocationTargetException;
|
|
|
import java.lang.reflect.Method;
|
|
import java.lang.reflect.Method;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -29,12 +30,6 @@ public class ExcelConverter implements Converter<String> {
|
|
|
return CellDataTypeEnum.STRING;
|
|
return CellDataTypeEnum.STRING;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //导入时转换
|
|
|
|
|
-// @Override
|
|
|
|
|
-// public StatusEnum convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
|
|
|
|
-// return null;
|
|
|
|
|
-// }
|
|
|
|
|
-
|
|
|
|
|
/**
|
|
/**
|
|
|
* 导出时转换
|
|
* 导出时转换
|
|
|
*
|
|
*
|
|
@@ -44,39 +39,6 @@ public class ExcelConverter implements Converter<String> {
|
|
|
* @return
|
|
* @return
|
|
|
* @throws Exception
|
|
* @throws Exception
|
|
|
*/
|
|
*/
|
|
|
-// @Override
|
|
|
|
|
-// public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
|
|
|
|
-// //value:状态码 contentProperty:字段属性 globalConfiguration:
|
|
|
|
|
-// //获取字段属性中的注解
|
|
|
|
|
-// Field field = contentProperty.getField();
|
|
|
|
|
-// EasyExcel easyExcel = field.getAnnotation(EasyExcel.class);
|
|
|
|
|
-// //获取注解中的枚举信息
|
|
|
|
|
-// Class<? extends Enum> type = easyExcel.type();
|
|
|
|
|
-// int status = easyExcel.status();
|
|
|
|
|
-// Object invoke = null;
|
|
|
|
|
-// if (NumberConstant.ONE == status) {
|
|
|
|
|
-// //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
-// Method codeOf = type.getMethod("getDestination", String.class);
|
|
|
|
|
-// //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
-// invoke = codeOf.invoke(type, value);
|
|
|
|
|
-// } else if (NumberConstant.TWO == status) {
|
|
|
|
|
-// //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
-// Method codeOf = type.getMethod("getName", Integer.class);
|
|
|
|
|
-// //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
-// invoke = codeOf.invoke(type, value);
|
|
|
|
|
-// } else {
|
|
|
|
|
-// //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
|
|
|
|
|
-// Method codeOf = type.getMethod("codeOf", Integer.class);
|
|
|
|
|
-// //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
-// invoke = codeOf.invoke(type, value);
|
|
|
|
|
-// //枚举实例调用getname方法,得到name的值
|
|
|
|
|
-// Method getName = invoke.getClass().getMethod("getName");
|
|
|
|
|
-// //将转换的值进行返回
|
|
|
|
|
-// invoke = String.valueOf(getName.invoke(invoke));
|
|
|
|
|
-// }
|
|
|
|
|
-// //将转换的值进行返回
|
|
|
|
|
-// return new WriteCellData(invoke.toString());
|
|
|
|
|
-// }
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
|
public WriteCellData<?> convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
|
|
|
//获取字段属性中的注解
|
|
//获取字段属性中的注解
|
|
@@ -89,26 +51,7 @@ public class ExcelConverter implements Converter<String> {
|
|
|
boolean flag = easyExcel.flag();
|
|
boolean flag = easyExcel.flag();
|
|
|
Object invoke = null;
|
|
Object invoke = null;
|
|
|
if (flag) {
|
|
if (flag) {
|
|
|
- if (NumberConstant.ONE == status) {
|
|
|
|
|
- //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
- Method codeOf = type.getMethod("getDestination", String.class);
|
|
|
|
|
- //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
- invoke = codeOf.invoke(type, value);
|
|
|
|
|
- } else if (NumberConstant.TWO == status) {
|
|
|
|
|
- //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
- Method codeOf = type.getMethod("getName", Integer.class);
|
|
|
|
|
- //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
- invoke = codeOf.invoke(type, Integer.getInteger(value));
|
|
|
|
|
- } else {
|
|
|
|
|
- //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
|
|
|
|
|
- Method codeOf = type.getMethod("getEntityByCode", Integer.class);
|
|
|
|
|
- //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
- invoke = codeOf.invoke(type, value);
|
|
|
|
|
- //枚举实例调用getname方法,得到name的值
|
|
|
|
|
- Method getName = invoke.getClass().getMethod("getDestination");
|
|
|
|
|
- //将转换的值进行返回
|
|
|
|
|
- invoke = String.valueOf(getName.invoke(invoke));
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ invoke = verifyTranslate(value, status, type);
|
|
|
} else {
|
|
} else {
|
|
|
//将转换的值进行返回
|
|
//将转换的值进行返回
|
|
|
return new WriteCellData(value.toString());
|
|
return new WriteCellData(value.toString());
|
|
@@ -117,4 +60,39 @@ public class ExcelConverter implements Converter<String> {
|
|
|
return new WriteCellData(invoke.toString());
|
|
return new WriteCellData(invoke.toString());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 应对不同枚举处理
|
|
|
|
|
+ * @param value
|
|
|
|
|
+ * @param status
|
|
|
|
|
+ * @param type
|
|
|
|
|
+ * @return
|
|
|
|
|
+ * @throws NoSuchMethodException
|
|
|
|
|
+ * @throws InvocationTargetException
|
|
|
|
|
+ * @throws IllegalAccessException
|
|
|
|
|
+ */
|
|
|
|
|
+ private Object verifyTranslate(String value, int status, Class<? extends Enum> type) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
|
|
|
|
|
+ Object invoke = null;
|
|
|
|
|
+ if (NumberConstant.ONE == status) {
|
|
|
|
|
+ //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
+ Method codeOf = type.getMethod("getDestination", String.class);
|
|
|
|
|
+ //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
+ invoke = codeOf.invoke(type, value);
|
|
|
|
|
+ } else if (NumberConstant.TWO == status) {
|
|
|
|
|
+ //获取枚举类的方法名 【destination】就是自己编写的函数,String.class 指定入参类型
|
|
|
|
|
+ Method codeOf = type.getMethod("getName", Integer.class);
|
|
|
|
|
+ //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
+ invoke = codeOf.invoke(type, Integer.getInteger(value));
|
|
|
|
|
+ } else {
|
|
|
|
|
+ //获取枚举类的方法名 “codeOf”就是自己编写的函数,Integer.class 指定入参类型
|
|
|
|
|
+ Method codeOf = type.getMethod("getEntityByCode", Integer.class);
|
|
|
|
|
+ //反射执行方法,此方法得到的是一个枚举实例(具体得到什么,结合自身项目)
|
|
|
|
|
+ invoke = codeOf.invoke(type, value);
|
|
|
|
|
+ //枚举实例调用getname方法,得到name的值
|
|
|
|
|
+ Method getName = invoke.getClass().getMethod("getDestination");
|
|
|
|
|
+ //将转换的值进行返回
|
|
|
|
|
+ invoke = String.valueOf(getName.invoke(invoke));
|
|
|
|
|
+ }
|
|
|
|
|
+ return invoke;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
}
|
|
}
|