From ca5b4bc852f83b9852d7e05a3a8ba02dbb2ced1e Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 11 Jan 2019 09:50:16 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/monitor/SysJobController.java | 2 +- .../monitor/SysJobLogController.java | 2 +- .../monitor/SysLogininforController.java | 2 +- .../monitor/SysOperlogController.java | 2 +- .../system/SysConfigController.java | 2 +- .../system/SysDictDataController.java | 2 +- .../system/SysDictTypeController.java | 2 +- .../controller/system/SysPostController.java | 2 +- .../controller/system/SysRoleController.java | 2 +- .../controller/system/SysUserController.java | 2 +- .../com/ruoyi/common/annotation/Excel.java | 6 +-- .../common/utils/{ => poi}/ExcelUtil.java | 37 ++++++++++--------- .../java/com/ruoyi/system/domain/SysUser.java | 12 +++--- 13 files changed, 39 insertions(+), 36 deletions(-) rename ruoyi-common/src/main/java/com/ruoyi/common/utils/{ => poi}/ExcelUtil.java (92%) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java index 973a30679..902e54063 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.quartz.domain.SysJob; import com.ruoyi.quartz.service.ISysJobService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobLogController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobLogController.java index 5c2cfc1df..523c34fbc 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobLogController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysJobLogController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.web.base.BaseController; import com.ruoyi.quartz.domain.SysJobLog; import com.ruoyi.quartz.service.ISysJobLogService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysLogininforController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysLogininforController.java index 13f3102f8..dcf5524ad 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysLogininforController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysLogininforController.java @@ -12,7 +12,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.SysLogininfor; import com.ruoyi.system.service.ISysLogininforService; import com.ruoyi.framework.web.base.BaseController; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysOperlogController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysOperlogController.java index c0cdbf9ef..34b3d46a6 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysOperlogController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/monitor/SysOperlogController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.system.domain.SysOperLog; import com.ruoyi.system.service.ISysOperLogService; import com.ruoyi.framework.web.base.BaseController; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java index 962fcf08c..db002154f 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysConfigController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysConfig; import com.ruoyi.system.service.ISysConfigService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictDataController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictDataController.java index de60f4a57..9b2bb2e10 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictDataController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictDataController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysDictData; import com.ruoyi.system.service.ISysDictDataService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java index f0e082a5f..454f191da 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysDictTypeController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysDictType; import com.ruoyi.system.service.ISysDictTypeService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysPostController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysPostController.java index f921464ad..66ab75bb7 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysPostController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysPostController.java @@ -14,7 +14,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysPost; import com.ruoyi.system.service.ISysPostService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java index 51d038163..bb1e7ab73 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysRoleController.java @@ -15,7 +15,7 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.system.domain.SysRole; import com.ruoyi.system.service.ISysRoleService; diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java index 391e4ea53..03b5a24aa 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysUserController.java @@ -16,8 +16,8 @@ import com.ruoyi.common.annotation.Log; import com.ruoyi.common.base.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.page.TableDataInfo; -import com.ruoyi.common.utils.ExcelUtil; import com.ruoyi.common.utils.StringUtils; +import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.framework.shiro.service.SysPasswordService; import com.ruoyi.framework.util.ShiroUtils; import com.ruoyi.framework.web.base.BaseController; 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 0cd77d214..98331b849 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 @@ -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) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java similarity index 92% rename from ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java rename to ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 2f3976eec..9c01a633e 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -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 */ public List importExcel(String sheetName, InputStream is) throws Exception { + this.type = Type.IMPORT; this.wb = new XSSFWorkbook(is); List list = new ArrayList(); Sheet sheet = null; @@ -161,8 +165,8 @@ public class ExcelUtil 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 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 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 // 得到所有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); } 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 22a666a2b..8c44e1077 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 @@ -1,10 +1,11 @@ package com.ruoyi.system.domain; -import org.apache.commons.lang3.builder.ToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; import java.util.Date; import java.util.List; +import org.apache.commons.lang3.builder.ToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; import com.ruoyi.common.annotation.Excel; +import com.ruoyi.common.annotation.Excel.Type; import com.ruoyi.common.base.BaseEntity; /** @@ -21,6 +22,7 @@ public class SysUser extends BaseEntity private Long userId; /** 部门ID */ + @Excel(name = "部门编号", type = Type.IMPORT) private Long deptId; /** 部门父ID */ @@ -63,15 +65,15 @@ public class SysUser extends BaseEntity private String delFlag; /** 最后登陆IP */ - @Excel(name = "最后登陆IP") + @Excel(name = "最后登陆IP", type = Type.EXPORT) private String loginIp; /** 最后登陆时间 */ - @Excel(name = "最后登陆时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + @Excel(name = "最后登陆时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT) private Date loginDate; /** 部门对象 */ - @Excel(name = "部门名称", targetAttr = "deptName") + @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT) private SysDept dept; private List roles;