Browse Source

支持枚举类型

4.1.3.A
jueyue 6 years ago
parent
commit
511e0bf2a6
10 changed files with 122 additions and 52 deletions
  1. +11
    -0
      easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java
  2. +2
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java
  3. +12
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java
  4. +12
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java
  5. +1
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java
  6. +4
    -0
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java
  7. +14
    -3
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java
  8. +17
    -20
      easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java
  9. +1
    -1
      easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java
  10. +48
    -27
      easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java

+ 11
- 0
easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java View File

@@ -167,5 +167,16 @@ public @interface Excel {
*/
public boolean isColumnHidden() default false;
/**
* 枚举导出使用的字段
* @return
*/
public String enumExportField() default "";
/**
* 枚举导入使用的函数
* @return
*/
public String enumImportMethod() default "";
}

+ 2
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java View File

@@ -161,4 +161,6 @@ public class ExcelBaseEntity {
public void setFixedIndex(Integer fixedIndex) {
this.fixedIndex = fixedIndex;
}
}

+ 12
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java View File

@@ -75,6 +75,10 @@ public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<Exc
* 是否隐藏列
*/
private boolean isColumnHidden;
/**
* 枚举导出属性字段
*/
private String enumExportField;
public boolean isColumnHidden() {
return isColumnHidden;
@@ -209,6 +213,14 @@ public class ExcelExportEntity extends ExcelBaseEntity implements Comparable<Exc
this.numFormat = numFormat;
}
public String getEnumExportField() {
return enumExportField;
}
public void setEnumExportField(String enumExportField) {
this.enumExportField = enumExportField;
}
@Override
public int compareTo(ExcelExportEntity prev) {
return this.getOrderNum() - prev.getOrderNum();


+ 12
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelImportEntity.java View File

@@ -48,6 +48,11 @@ public class ExcelImportEntity extends ExcelBaseEntity {
*/
private boolean importField;
/**
* 枚举导入静态方法
*/
private String enumImportMethod;
private List<ExcelImportEntity> list;
public String getClassType() {
@@ -106,4 +111,11 @@ public class ExcelImportEntity extends ExcelBaseEntity {
this.importField = importField;
}
public String getEnumImportMethod() {
return enumImportMethod;
}
public void setEnumImportMethod(String enumImportMethod) {
this.enumImportMethod = enumImportMethod;
}
}

+ 1
- 1
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java View File

@@ -403,7 +403,7 @@ public abstract class BaseExportService extends ExportCommonService {
if (excelParams.get(i).getList() != null) {
List<ExcelExportEntity> list = excelParams.get(i).getList();
for (int j = 0; j < list.size(); j++) {
sheet.setColumnHidden(index,list.get(i).isColumnHidden());
sheet.setColumnHidden(index,list.get(j).isColumnHidden());
index++;
}
} else {


+ 4
- 0
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java View File

@@ -206,6 +206,9 @@ public class ExportCommonService {
if (StringUtils.isNotEmpty(entity.getSuffix()) && value != null) {
value = value + entity.getSuffix();
}
if (value != null && StringUtils.isNotEmpty(entity.getEnumExportField())){
value = PoiReflectorUtil.fromCache(value.getClass()).getValue(value,entity.getEnumExportField());
}
return value == null ? "" : value.toString();
}

@@ -249,6 +252,7 @@ public class ExportCommonService {
excelEntity.setNumFormat(excel.numFormat());
excelEntity.setColumnHidden(excel.isColumnHidden());
excelEntity.setDict(excel.dict());
excelEntity.setEnumExportField(excel.enumExportField());
if (excelGroup != null) {
excelEntity.setGroupName(PoiPublicUtil.getValueByTargetId(excelGroup.name(), targetId, null));
} else {


+ 14
- 3
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java View File

@@ -28,6 +28,7 @@ import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.handler.inter.IExcelDictHandler;
import cn.afterturn.easypoi.util.PoiReflectorUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
@@ -191,11 +192,13 @@ public class CellValueService {
String titleString, IExcelDictHandler dictHandler) throws Exception {
ExcelImportEntity entity = excelParams.get(titleString);
String xclass = "class java.lang.Object";
Class clazz = null;
if (!(object instanceof Map)) {
Method setMethod = entity.getMethods() != null && entity.getMethods().size() > 0
? entity.getMethods().get(entity.getMethods().size() - 1) : entity.getMethod();
Type[] ts = setMethod.getGenericParameterTypes();
xclass = ts[0].toString();
clazz = (Class) ts[0];
}
Object result = getCellValue(xclass, cell, entity);
if (entity != null) {
@@ -207,7 +210,7 @@ public class CellValueService {
}
}
result = handlerValue(dataHandler, object, result, titleString);
return getValueByType(xclass, result, entity);
return getValueByType(xclass, result, entity, clazz);
}

/**
@@ -231,7 +234,7 @@ public class CellValueService {
result = hanlderSuffix(entity.getSuffix(), result);
result = replaceValue(entity.getReplace(), result);
result = handlerValue(dataHandler, object, result, titleString);
return getValueByType(xclass, result, entity);
return getValueByType(xclass, result, entity, (Class) ts[0]);
}

/**
@@ -255,9 +258,10 @@ public class CellValueService {
* @param xclass
* @param result
* @param entity
* @param clazz
* @return
*/
private Object getValueByType(String xclass, Object result, ExcelImportEntity entity) {
private Object getValueByType(String xclass, Object result, ExcelImportEntity entity, Class clazz) {
try {
//过滤空和空字符串,如果基本类型null会在上层抛出,这里就不处理了
if (result == null || StringUtils.isBlank(result.toString())) {
@@ -305,6 +309,13 @@ public class CellValueService {
}
return String.valueOf(result);
}
if (clazz.isEnum()) {
if (StringUtils.isNotEmpty(entity.getEnumImportMethod())) {
return PoiReflectorUtil.fromCache(clazz).execEnumStaticMethod(entity.getEnumImportMethod(),result);
} else {
return Enum.valueOf(clazz,result.toString());
}
}
return result;
} catch (Exception e) {
LOGGER.error(e.getMessage(), e);


+ 17
- 20
easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java View File

@@ -77,7 +77,21 @@ public class ImportBaseService {
excelEntity.setImportField(Boolean
.valueOf(PoiPublicUtil.getValueByTargetId(excel.isImportField(), targetId, "false")));
excelEntity.setFixedIndex(excel.fixedIndex());
getExcelField(targetId, field, excelEntity, excel, pojoClass, excelEntityAnn);
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null));
if (StringUtils.isNoneEmpty(excel.groupName())) {
excelEntity.setName(excel.groupName() + "_" + excelEntity.getName());
}
if(excelEntityAnn != null && excelEntityAnn.show()){
excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName());
}
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(field.getName()));
if (StringUtils.isNotEmpty(excel.importFormat())) {
excelEntity.setFormat(excel.importFormat());
} else {
excelEntity.setFormat(excel.format());
}
excelEntity.setDict(excel.dict());
excelEntity.setEnumImportMethod(excel.enumImportMethod());
if (getMethods != null) {
List<Method> newMethods = new ArrayList<Method>();
newMethods.addAll(getMethods);
@@ -128,7 +142,7 @@ public class ImportBaseService {
field.getAnnotation(ExcelCollection.class).name(), targetId, null));
additionalCollectionName(collection);
excelCollection.add(collection);
} else if (PoiPublicUtil.isJavaClass(field)) {
} else if (PoiPublicUtil.isJavaClass(field) || field.getType().isEnum()) {
addEntityToMap(targetId, field, excelEntity, pojoClass, getMethods, excelParams, excelEntityAnn);
} else {
List<Method> newMethods = new ArrayList<Method>();
@@ -162,24 +176,7 @@ public class ImportBaseService {
}
}
public void getExcelField(String targetId, Field field, ExcelImportEntity excelEntity,
Excel excel, Class<?> pojoClass, ExcelEntity excelEntityAnn) throws Exception {
excelEntity.setName(PoiPublicUtil.getValueByTargetId(excel.name(), targetId, null));
if (StringUtils.isNoneEmpty(excel.groupName())) {
excelEntity.setName(excel.groupName() + "_" + excelEntity.getName());
}
if(excelEntityAnn != null && excelEntityAnn.show()){
excelEntity.setName(excelEntityAnn.name() + "_" + excelEntity.getName());
}
String fieldname = field.getName();
excelEntity.setMethod(PoiReflectorUtil.fromCache(pojoClass).getSetMethod(fieldname));
if (StringUtils.isNotEmpty(excel.importFormat())) {
excelEntity.setFormat(excel.importFormat());
} else {
excelEntity.setFormat(excel.format());
}
excelEntity.setDict(excel.dict());
}
public void getExcelFieldList(String targetId, Field[] fields, Class<?> pojoClass,
Map<String, ExcelImportEntity> temp,


+ 1
- 1
easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java View File

@@ -105,7 +105,7 @@ public final class PoiPublicUtil {
ExcelCollection collection = field.getAnnotation(ExcelCollection.class);
PoiReflectorUtil.fromCache(clazz).setValue(obj, field.getName(),
collection.type().newInstance());
} else if (!isJavaClass(field)) {
} else if (!isJavaClass(field) && !field.getType().isEnum()) {
PoiReflectorUtil.fromCache(clazz).setValue(obj, field.getName(),
createObject(field.getType(), targetId));
}


+ 48
- 27
easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java View File

@@ -1,6 +1,7 @@
package cn.afterturn.easypoi.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ReflectPermission;
import java.util.ArrayList;
@@ -14,20 +15,23 @@ import java.util.concurrent.ConcurrentHashMap;
/**
* 反射工具类,缓存读取的class信息,省的一直获取
* @author JueYue
* 参考的 mybatis的Reflector
*
* @author JueYue
* 参考的 mybatis的Reflector
*/
public final class PoiReflectorUtil {
private static final Map<Class<?>, PoiReflectorUtil> CACHE_REFLECTOR = new ConcurrentHashMap<Class<?>, PoiReflectorUtil>();
private Map<String, Method> getMethods = new HashMap<String, Method>();
private Map<String, Method> setMethods = new HashMap<String, Method>();
private List<Field> fieldList = new ArrayList<Field>();
private Map<String, Method> getMethods = new HashMap<String, Method>();
private Map<String, Method> setMethods = new HashMap<String, Method>();
private Map<String, Method> enumMethods = new HashMap<String, Method>();
private List<Field> fieldList = new ArrayList<Field>();
private Class<?> type;
private Class<?> type;
private PoiReflectorUtil(Class<?> clazz) {
this.type = clazz;
addGetMethods(clazz);
addFields(clazz);
addSetMethods(clazz);
@@ -79,11 +83,11 @@ public final class PoiReflectorUtil {
Class<?> methodType = method.getReturnType();
if (methodType.equals(getterType)) {
throw new RuntimeException(
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
} else if (methodType.isAssignableFrom(getterType)) {
// OK getter type is descendant
} else if (getterType.isAssignableFrom(methodType)) {
@@ -91,11 +95,11 @@ public final class PoiReflectorUtil {
getterType = methodType;
} else {
throw new RuntimeException(
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
"Illegal overloaded getter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
}
}
addGetMethod(propName, getter);
@@ -131,7 +135,7 @@ public final class PoiReflectorUtil {
name = name.substring(3);
} else {
throw new RuntimeException("Error parsing property name '" + name
+ "'. Didn't start with 'is', 'get' or 'set'.");
+ "'. Didn't start with 'is', 'get' or 'set'.");
}
if (name.length() == 1 || (name.length() > 1 && !Character.isUpperCase(name.charAt(1)))) {
@@ -169,11 +173,11 @@ public final class PoiReflectorUtil {
}
if (setter == null) {
throw new RuntimeException(
"Illegal overloaded setter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
"Illegal overloaded setter method with ambiguous type for property "
+ propName + " in class "
+ firstMethod.getDeclaringClass()
+ ". This breaks the JavaBeans "
+ "specification and can cause unpredicatble results.");
}
addSetMethod(propName, setter);
}
@@ -296,7 +300,7 @@ public final class PoiReflectorUtil {
Method method = getMethods.get(propertyName);
if (method == null) {
throw new RuntimeException(
"There is no getter for property named '" + propertyName + "' in '" + type + "'");
"There is no getter for property named '" + propertyName + "' in '" + type + "'");
}
return method;
}
@@ -305,13 +309,14 @@ public final class PoiReflectorUtil {
Method method = setMethods.get(propertyName);
if (method == null) {
throw new RuntimeException(
"There is no getter for property named '" + propertyName + "' in '" + type + "'");
"There is no getter for property named '" + propertyName + "' in '" + type + "'");
}
return method;
}
/**
* 获取field 值
*
* @param obj
* @param property
* @return
@@ -321,7 +326,7 @@ public final class PoiReflectorUtil {
Method m = getMethods.get(property);
if (m != null) {
try {
value = m.invoke(obj, new Object[] {});
value = m.invoke(obj, new Object[]{});
} catch (Exception ex) {
}
}
@@ -330,9 +335,10 @@ public final class PoiReflectorUtil {
/**
* 设置field值
* @param obj 对象
*
* @param obj 对象
* @param property
* @param object 属性值
* @param object 属性值
* @return
*/
public boolean setValue(Object obj, String property, Object object) {
@@ -356,4 +362,19 @@ public final class PoiReflectorUtil {
return fieldList;
}
public Object execEnumStaticMethod(String staticMethod, Object params) {
if (!enumMethods.containsKey(setMethods)) {
try {
enumMethods.put(staticMethod,type.getMethod(staticMethod,params.getClass()));
} catch (NoSuchMethodException e) {
throw new RuntimeException(
"There is no enum for property named '" + staticMethod + "' in '" + type + "'");
}
}
try {
return enumMethods.get(staticMethod).invoke(null,params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Loading…
Cancel
Save