From 511e0bf2a63ce81a55ef7bcf73dcd5f06f076c71 Mon Sep 17 00:00:00 2001 From: jueyue Date: Mon, 2 Apr 2018 14:08:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=E6=9E=9A=E4=B8=BE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../easypoi/excel/annotation/Excel.java | 11 +++ .../excel/entity/params/ExcelBaseEntity.java | 2 + .../entity/params/ExcelExportEntity.java | 12 +++ .../entity/params/ExcelImportEntity.java | 12 +++ .../excel/export/base/BaseExportService.java | 2 +- .../export/base/ExportCommonService.java | 4 + .../excel/imports/CellValueService.java | 17 ++++- .../excel/imports/base/ImportBaseService.java | 37 +++++---- .../afterturn/easypoi/util/PoiPublicUtil.java | 2 +- .../easypoi/util/PoiReflectorUtil.java | 75 ++++++++++++------- 10 files changed, 122 insertions(+), 52 deletions(-) diff --git a/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java b/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java index 04ece3f..02c6cf2 100644 --- a/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java +++ b/easypoi-annotation/src/main/java/cn/afterturn/easypoi/excel/annotation/Excel.java @@ -167,5 +167,16 @@ public @interface Excel { */ public boolean isColumnHidden() default false; + /** + * 枚举导出使用的字段 + * @return + */ + public String enumExportField() default ""; + /** + * 枚举导入使用的函数 + * @return + */ + public String enumImportMethod() default ""; + } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java index 9402a3f..b07f272 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelBaseEntity.java @@ -161,4 +161,6 @@ public class ExcelBaseEntity { public void setFixedIndex(Integer fixedIndex) { this.fixedIndex = fixedIndex; } + + } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java index 98d3bde..ad198e0 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/entity/params/ExcelExportEntity.java @@ -75,6 +75,10 @@ public class ExcelExportEntity extends ExcelBaseEntity implements Comparable 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; + } } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java index 9ac71c9..6764c7e 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/BaseExportService.java @@ -403,7 +403,7 @@ public abstract class BaseExportService extends ExportCommonService { if (excelParams.get(i).getList() != null) { List 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 { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java index 638b15d..dc23e4f 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/export/base/ExportCommonService.java @@ -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 { diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java index 8e40e30..63f30ec 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/CellValueService.java @@ -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); diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java index f1344fb..e29c7a2 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/excel/imports/base/ImportBaseService.java @@ -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 newMethods = new ArrayList(); 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 newMethods = new ArrayList(); @@ -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 temp, diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java index dc012e6..5c3b7e8 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiPublicUtil.java @@ -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)); } diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java index 4d6d5cc..78a1e29 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/util/PoiReflectorUtil.java @@ -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, PoiReflectorUtil> CACHE_REFLECTOR = new ConcurrentHashMap, PoiReflectorUtil>(); - private Map getMethods = new HashMap(); - private Map setMethods = new HashMap(); - private List fieldList = new ArrayList(); + private Map getMethods = new HashMap(); + private Map setMethods = new HashMap(); + private Map enumMethods = new HashMap(); + private List fieldList = new ArrayList(); - 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); + } + } }