Open FlashMickeyMouse opened 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;
}
顶
感谢提供代码,暂时不考虑将字段设置为枚举类型,但是会将此代码作为poi扩展篇提供给其他需要的人,再次感谢
导入导出时没有考虑字段类型是枚举的情况,在此指出忘修正。 @see org.jeecgframework.poi.util.PoiPublicUtil#createObject