用户导入

This commit is contained in:
RuoYi
2019-01-11 09:50:16 +08:00
parent 5fc9a63231
commit ca5b4bc852
13 changed files with 39 additions and 36 deletions

View File

@ -70,13 +70,13 @@ public @interface Excel
public String targetAttr() default "";
/**
* 字段类型( 1仅导出2仅导入
* 字段类型(0导出导入1仅导出2仅导入
*/
Type type() default Type.EXPORT;
Type type() default Type.ALL;
public enum Type
{
EXPORT(1), IMPORT(2);
ALL(0), EXPORT(1), IMPORT(2);
private final int value;
Type(int value)

View File

@ -1,4 +1,4 @@
package com.ruoyi.common.utils;
package com.ruoyi.common.utils.poi;
import java.io.File;
import java.io.FileOutputStream;
@ -41,6 +41,9 @@ import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.reflect.ReflectUtils;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
/**
* Excel相关处理
@ -129,6 +132,7 @@ public class ExcelUtil<T>
*/
public List<T> importExcel(String sheetName, InputStream is) throws Exception
{
this.type = Type.IMPORT;
this.wb = new XSSFWorkbook(is);
List<T> list = new ArrayList<T>();
Sheet sheet = null;
@ -161,8 +165,8 @@ public class ExcelUtil<T>
for (int col = 0; col < allFields.length; col++)
{
Field field = allFields[col];
// 将有注解的field存放到map中.
if (field.isAnnotationPresent(Excel.class))
Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
{
// 设置类的私有字段属性可访问.
field.setAccessible(true);
@ -187,31 +191,31 @@ public class ExcelUtil<T>
Class<?> fieldType = field.getType();
if (String.class == fieldType)
{
String s = String.valueOf(val.toString());
String s = Convert.toStr(val);
if (StringUtils.endsWith(s, ".0"))
{
val = StringUtils.substringBefore(s, ".0");
}
else
{
val = String.valueOf(val.toString());
val = Convert.toStr(val);
}
}
else if ((Integer.TYPE == fieldType) || (Integer.class == fieldType))
{
val = Double.valueOf(val.toString()).intValue();
val = Convert.toInt(val);
}
else if ((Long.TYPE == fieldType) || (Long.class == fieldType))
{
val = Double.valueOf(val.toString()).longValue();
val = Convert.toLong(val);
}
else if ((Double.TYPE == fieldType) || (Double.class == fieldType))
{
val = Double.valueOf(val.toString());
val = Convert.toDouble(val);
}
else if ((Float.TYPE == fieldType) || (Float.class == fieldType))
{
val = Float.valueOf(val.toString());
val = Convert.toFloat(val);
}
else if (Date.class == fieldType)
{
@ -227,20 +231,16 @@ public class ExcelUtil<T>
if (StringUtils.isNotNull(fieldType))
{
Excel attr = field.getAnnotation(Excel.class);
String propertyName = field.getName();
if (StringUtils.isNotEmpty(attr.targetAttr()))
{
ReflectUtils.invokeSetter(entity, field.getName() + "." + attr.targetAttr(), val);
propertyName = field.getName() + "." + attr.targetAttr();
}
else if (StringUtils.isNotEmpty(attr.readConverterExp()))
{
String value = reverseByExp(String.valueOf(val), attr.readConverterExp());
ReflectUtils.invokeSetter(entity, field.getName() + "." + attr.targetAttr(), value);
}
else
{
ReflectUtils.invokeSetter(entity, field.getName(), val);
val = reverseByExp(String.valueOf(val), attr.readConverterExp());
}
ReflectUtils.invokeSetter(entity, propertyName, val);
}
}
list.add(entity);
@ -653,7 +653,8 @@ public class ExcelUtil<T>
// 得到所有field并存放到一个list中.
for (Field field : allFields)
{
if (field.isAnnotationPresent(Excel.class))
Excel attr = field.getAnnotation(Excel.class);
if (attr != null && (attr.type() == Type.ALL || attr.type() == type))
{
fields.add(field);
}