Browse Source

Excel注解支持多级获取

pull/58/MERGE
RuoYi 6 years ago
parent
commit
3d3017804d
  1. 5
      ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java
  2. 60
      ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java
  3. 1
      ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java

5
ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java

@ -63,4 +63,9 @@ public @interface Excel
* 是否导出数据,应对需求:有时我们需要导出一份模板,这是标题需要但内容需要用户手工填写.
*/
public boolean isExport() default true;
/**
* 另一个类中的属性名称,支持多级获取,以小数点隔开
*/
public String targetAttr() default "";
}

60
ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java

@ -6,6 +6,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -344,21 +345,23 @@ public class ExcelUtil<T>
continue;
}
// 用于读取对象中的属性
Object value = getTargetValue(vo, field, attr);
String dateFormat = attr.dateFormat();
String readConverterExp = attr.readConverterExp();
if (StringUtils.isNotEmpty(dateFormat))
{
cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) field.get(vo)));
cell.setCellValue(DateUtils.parseDateToStr(dateFormat, (Date) value));
}
else if (StringUtils.isNotEmpty(readConverterExp))
{
cell.setCellValue(convertByExp(String.valueOf(field.get(vo)), readConverterExp));
cell.setCellValue(convertByExp(String.valueOf(value), readConverterExp));
}
else
{
cell.setCellType(CellType.STRING);
// 如果数据存在就填入,不存在填入空格.
cell.setCellValue(StringUtils.isNull(field.get(vo)) ? attr.defaultValue() : field.get(vo) + attr.suffix());
cell.setCellValue(StringUtils.isNull(value) ? attr.defaultValue() : value + attr.suffix());
}
}
}
@ -509,4 +512,55 @@ public class ExcelUtil<T>
}
return downloadPath;
}
/**
* 获取bean中的属性值
*
* @param vo 实体对象
* @param field 字段
* @param excel 注解
* @return 最终的属性值
* @throws Exception
*/
private Object getTargetValue(T vo, Field field, Excel excel) throws Exception
{
Object o = field.get(vo);
if (StringUtils.isNotEmpty(excel.targetAttr()))
{
String target = excel.targetAttr();
if (target.indexOf(".") > -1)
{
String[] targets = target.split("[.]");
for (String name : targets)
{
o = getValue(o, name);
}
}
else
{
o = getValue(o, target);
}
}
return o;
}
/**
* 以类的属性的get方法方法形式获取值
*
* @param o
* @param name
* @return value
* @throws Exception
*/
private Object getValue(Object o, String name) throws Exception
{
if (StringUtils.isNotEmpty(name))
{
Class<?> clazz = o.getClass();
String methodName = "get" + name.substring(0, 1).toUpperCase() + name.substring(1);
Method method = clazz.getMethod(methodName);
o = method.invoke(o);
}
return o;
}
}

1
ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java

@ -71,6 +71,7 @@ public class SysUser extends BaseEntity
private Date loginDate;
/** 部门对象 */
@Excel(name = "部门名称", targetAttr = "deptName")
private SysDept dept;
private List<SysRole> roles;

Loading…
Cancel
Save