JeeSite的Excel導入、導出、支持大數據量,使用annotation最小化配置

JeeSite的Excel導入、導出、支持大數據量,使用annotation最小化配置

介紹:

對Apache POI 3.9的簡單封裝,實現Excel的導出導入功能。使用Annotation定義導出導入字段。http://jeesite.comhtml

優勢:

  1. 簡單易用,支持大數量導出,配置簡單,代碼量少。
  2. 支持Excel 200三、200七、2010(xls、xlsx)格式。
  3. 支持簡單格式設置,對齊方式,排序等
  4. 可導出字典類型數據,自定義數據字段類型(例如:部門關聯對象,部門名稱與部門編號互轉)。
  5. 無需創建導入模板,系統自動生成。

缺點:

  1. 格式單一,沒法導出格式比較複雜的表格。
  2. 不能使用模板進行導入,導出。

使用示例:

 

一、導出實體對象中的annotation的定義(ExcelField說明見:五、ExcelField定義說明):java

  

  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. }  

 

 二、Excel導出示例:git

 

  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. }  

 

三、Excel 導入示例:github

 

  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, 1, 0);  
  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. }  

 

四、Excel 導入模板下載示例app

 

  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.class, 2).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. }  

 

  

五、ExcelField定義說明:大數據

 

 

  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.       
相關文章
相關標籤/搜索