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