JeeSite的Excel导入、导出

来源:互联网 发布:忘了淘宝账号怎么办 编辑:程序博客网 时间:2024/04/29 21:35

JeeSite的Excel导入、导出、支持大数据量,应用annotation最小化配置


jeeSite的Excel导入、导出、支持大数据量,使用annotation最小化配置

介绍:

对Apache POI 3.9的简单封装,实现Excel的导出导入功能。使用Annotation定义导出导入字段。http://jeesite.com

优点:

  1. 简单易用,支持大数量导出,配置简单,代码量少。
  2. 支持Excel 2003、2007、2010(xls、xlsx)格式。
  3. 支持简单格式设置,对齐方式,排序等
  4. 可导出字典类型数据,自定义数据字段类型(例如:部门关联对象,部门名称与部门编号互转)。
  5. 无需建立导入模板,系统自动生成。

缺点:

  1. 格式单一,无法导出格式比较复杂的表格。
  2. 不能使用模板进行导入,导出。

使用示例:

 

1、导出实体对象中的annotation的定义(ExcelField说明见:5、ExcelField定义说明):

  

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. @Entity  
  2. @Table(name = "sys_user")  
  3. public class User extends BaseEntity {  
  4.   
  5.     private Long id;        // 编号  
  6.     ...  
  7.     ...  
  8.     ...   
  9.     private List<Role> roleList = Lists.newArrayList(); // 拥有角色列表  
  10.       
  11.     @Id  
  12.     @ExcelField(title="ID", type=1, align=2, sort=1)  
  13.     public Long getId() {  
  14.         return id;  
  15.     }  
  16.     @ManyToOne  
  17.     @ExcelField(title="所属区域", align=2, sort=10)  
  18.     public Area getArea() {  
  19.         return area;  
  20.     }  
  21.     @ManyToOne  
  22.     @ExcelField(title="所属部门", align=2, sort=20)  
  23.     public Office getOffice() {  
  24.         return office;  
  25.     }  
  26.     @Length(min=1, max=100)  
  27.     @ExcelField(title="姓名", align=2, sort=40)  
  28.     public String getName() {  
  29.         return name;  
  30.     }  
  31.     @Length(min=0, max=100)  
  32.     @ExcelField(title="用户类型", align=2, sort=80, dictType="sys_user_type")  
  33.     public String getUserType() {  
  34.         return userType;  
  35.     }  
  36.     @ExcelField(title="创建时间", type=0, align=1, sort=90)  
  37.     public Date getCreateDate() {  
  38.         return createDate;  
  39.     }  
  40.     @ExcelField(title="最后登录日期", type=1, align=1, sort=110)  
  41.     public Date getLoginDate() {  
  42.         return loginDate;  
  43.     }  
  44.     @ManyToMany  
  45.     @ExcelField(title="拥有角色", align=1, sort=800, fieldType=RoleListType.class)  
  46.     public List<Role> getRoleList() {  
  47.         return roleList;  
  48.     }  
  49. }  
 

 2、Excel导出示例:

 

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public String exportFile(User user) {  
  2.     try {  
  3.         String fileName = "用户数据"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";   
  4.                 // 查询数据  
  5.         Page<User> page = systemService.findUser(new Page<User>(request, response, -1), user);   
  6.                 // 1:创建Excel导出对象;2:设置数据;3:写入输出流;4:临时数据销毁  
  7.         new ExportExcel("用户数据", User.class)  
  8.                      .setDataList(page.getList())  
  9.                      .write(response, fileName)  
  10.                      .dispose();  
  11.         return null;  
  12.     } catch (Exception e) {  
  13.         addFlashMessage("导出用户失败!失败信息:"+e.getMessage());  
  14.     }  
  15.     return "redirect:"+BaseController.ADMIN_PATH+"/sys/user/?repage";  
  16. }  

 

3、Excel 导入示例:

 

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public String importFile(MultipartFile file) {  
  2.     try {  
  3.         int successNum = 0;  
  4.         int failureNum = 0;  
  5.         StringBuilder failureMsg = new StringBuilder();  
  6.                 // 创建导入Excel对象  
  7.         ImportExcel ei = new ImportExcel(file, 10);  
  8.                 // 获取传入Excel文件的数据,根据传入参数类型,自动转换为对象  
  9.         List<User> list = ei.getDataList(User.class);  
  10.                 // 遍历数据,保存数据  
  11.         for (User user : list){  
  12.             try{  
  13.                 if ("true".equals(checkLoginName("", user.getLoginName()))){  
  14.                     user.setPassword(SystemService.entryptPassword("123456"));  
  15.                     BeanValidators.validateWithException(validator, user);  
  16.                     systemService.saveUser(user);  
  17.                     successNum++;  
  18.                 }else{  
  19.                     failureMsg.append("<br/>登录名 "+user.getLoginName()+" 已存在; ");  
  20.                     failureNum++;  
  21.                 }  
  22.             }catch(ConstraintViolationException ex){  
  23.                 failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败:");  
  24.                 List<String> messageList = BeanValidators.extractPropertyAndMessageAsList(ex, ": ");  
  25.                 for (String message : messageList){  
  26.                     failureMsg.append(message+"; ");  
  27.                     failureNum++;  
  28.                 }  
  29.             }catch (Exception ex) {  
  30.                 failureMsg.append("<br/>登录名 "+user.getLoginName()+" 导入失败:"+ex.getMessage());  
  31.             }  
  32.         }  
  33.         if (failureNum>0){  
  34.             failureMsg.insert(0",失败 "+failureNum+" 条用户,导入信息如下:");  
  35.         }  
  36.         addFlashMessage("已成功导入 "+successNum+" 条用户"+failureMsg);  
  37.     } catch (Exception e) {  
  38.         addFlashMessage("导入用户失败!失败信息:"+e.getMessage());  
  39.     }  
  40.     return "redirect:"+BaseController.ADMIN_PATH+"/sys/user/?repage";  
  41. }  

 

4、Excel 导入模板下载示例

 

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. public String importFileTemplate() {  
  2.     try {  
  3.                 String fileName = "用户数据导入模板.xlsx";  
  4.         List<User> list = Lists.newArrayList(); list.add(UserUtils.getUser(true));  
  5.                 // 第三个参数设置为“2”表示输出为导入模板(1:导出数据;2:导入模板)  
  6.         new ExportExcel("用户数据", User.class2).setDataList(list).write(response, fileName).dispose();  
  7.         return null;  
  8.     } catch (Exception e) {  
  9.         addFlashMessage("导出用户失败!失败信息:"+e.getMessage());  
  10.     }  
  11.     return "redirect:"+BaseController.ADMIN_PATH+"/sys/user/?repage";  
  12. }  

 

  

5、ExcelField定义说明:

 

 

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  */  
  6. package com.thinkgem.jeesite.common.utils.excel.annotation;  
  7.   
  8. import java.lang.annotation.ElementType;  
  9. import java.lang.annotation.Retention;  
  10. import java.lang.annotation.RetentionPolicy;  
  11. import java.lang.annotation.Target;  
  12.   
  13. /** 
  14.  * Excel注解定义 
  15.  * @author ThinkGem 
  16.  * @version 2013-03-10 
  17.  */  
  18. @Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})  
  19. @Retention(RetentionPolicy.RUNTIME)  
  20. public @interface ExcelField {  
  21.   
  22.     /** 
  23.      * 导出字段名(默认调用当前字段的“get”方法,如指定导出字段为对象,请填写“对象名.对象属性”,例:“area.name”、“office.name”) 
  24.      */  
  25.     String value() default "";  
  26.       
  27.     /** 
  28.      * 导出字段标题 
  29.      */  
  30.     String title();  
  31.       
  32.     /** 
  33.      * 字段类型(0:导出导入;1:仅导出;2:仅导入) 
  34.      */  
  35.     int type() default 0;  
  36.   
  37.     /** 
  38.      * 导出字段对齐方式(0:自动;1:靠左;2:居中;3:靠右) 
  39.      */  
  40.     int align() default 0;  
  41.       
  42.     /** 
  43.      * 导出字段字段排序(升序) 
  44.      */  
  45.     int sort() default 0;  
  46.   
  47.     /** 
  48.      * 如果是字典类型,请设置字典的type值 
  49.      */  
  50.     String dictType() default "";  
  51.       
  52.     /** 
  53.      * 反射类型 
  54.      */  
  55.     Class<?> fieldType() default Class.class;  
  56.       
  57. }  
1 0
原创粉丝点击