From 5724b1a1b968144aec78d4fc25c0c22bbefd6525 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 28 Dec 2018 16:15:56 +0800 Subject: [PATCH] =?UTF-8?q?Excel=E6=B3=A8=E8=A7=A3=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E5=A4=9A=E7=BA=A7=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/annotation/Excel.java | 5 ++ .../com/ruoyi/common/utils/ExcelUtil.java | 62 +++++++++++++++++-- .../java/com/ruoyi/system/domain/SysUser.java | 1 + 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java b/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java index 20f86a57..7a925a16 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/annotation/Excel.java +++ b/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 ""; } \ No newline at end of file diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java index 60f6cef5..38f0790e 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java +++ b/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 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()); } } } @@ -455,7 +458,7 @@ public class ExcelUtil sheet.addValidationData(dataValidationList); return sheet; } - + /** * 解析导出值 0=男,1=女,2=未知 * @@ -509,4 +512,55 @@ public class ExcelUtil } 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; + } } \ No newline at end of file diff --git a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java b/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java index 9b40ba48..bca493a8 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/system/domain/SysUser.java +++ b/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 roles;