jeecgboot / autopoi

AutoPOI 功能如同名字auto,追求的就是自动化,让一个没接触过poi的人员,可以傻瓜化的快速实现Excel导入导出、Word模板导出、可以仅仅5行代码就可以完成Excel的导入导出。
http://www.jeecg.com
Apache License 2.0
445 stars 197 forks source link

导入导出时没有考虑字段类型是枚举的情况 #18

Open FlashMickeyMouse opened 4 years ago

FlashMickeyMouse commented 4 years ago

导入导出时没有考虑字段类型是枚举的情况,在此指出忘修正。 @see org.jeecgframework.poi.util.PoiPublicUtil#createObject

tygithub1 commented 4 years ago

目前没有计划设置字段类型为枚举

FlashMickeyMouse commented 4 years ago

如果可以的话,是否可以应用我的更改方案: 代码变更如下 1.org.jeecgframework.poi.excel.imports.base.ImportBaseService#setValues

/**
     * @param entity
     * @param object
     * @param value
     * @mender songhao  sh_hq_123@163.com
     * @throws Exception
     */
public void setValues(ExcelImportEntity entity, Object object, Object value) throws Exception {
        if (entity.getMethods() != null) {
            setFieldBySomeMethod(entity.getMethods(), object, value);
        } else {
            if (entity.getMethod().getParameterTypes()[0].isEnum()) {
                entity.getMethod().invoke(object, Enum.valueOf((Class) entity.getMethod().getParameterTypes()[0], value.toString()));
            } else {
                entity.getMethod().invoke(object, value);
            }
        }
    }

2.org.jeecgframework.poi.util.PoiPublicUtil#createObject

/**
     * 彻底创建一个对象
     *
     * @param clazz
     * @mender songhao  sh_hq_123@163.com
     * @return
     */
    public static Object createObject(Class<?> clazz, String targetId) {
        Object obj = null;
        Method setMethod;
        try {
            if (clazz.equals(Map.class)) {
                return new HashMap<String, Object>();
            }
            obj = clazz.newInstance();
            Field[] fields = getClassFields(clazz);
            for (Field field : fields) {
                if (isNotUserExcelUserThis(null, field, targetId)) {
                    continue;
                }
                if (isCollection(field.getType())) {
                    ExcelCollection collection = field.getAnnotation(ExcelCollection.class);
                    setMethod = getMethod(field.getName(), clazz, field.getType());
                    setMethod.invoke(obj, collection.type().newInstance());
                } else if (field.getType().isEnum()) {
                    getMethod(field.getName(), clazz, field.getType());
                } else if (!isJavaClass(field)) {

                    setMethod = getMethod(field.getName(), clazz, field.getType());
                    setMethod.invoke(obj, createObject(field.getType(), targetId));
                }
            }

        } catch (Exception e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException("创建对象异常");
        }
        return obj;

    }
ggeden commented 4 years ago

tygithub1 commented 3 years ago

感谢提供代码,暂时不考虑将字段设置为枚举类型,但是会将此代码作为poi扩展篇提供给其他需要的人,再次感谢