EasyPoi 多sheet匯出功能實現
阿新 • • 發佈:2018-12-23
EasyPoi 多sheet匯出
序言:之前一直想開始寫部落格,都沒有時間行動起來,今天終於開始了我的第一篇部落格…
最近接到一個匯出excel功能的需求,該功能主要難點是
- 多sheet頁匯出
- 合併單元格(跨行、跨列)
- 多表頭合併
我開始的想法是如果採用poi來實現這個功能,業務邏輯可能會有點複雜,於是我使用了easyPoi——一個so easy的工具,它的特點就是非常方便,用jQuery的一句來說就是:write Less,Do More。
話不多說,接下來分享一下我的程式碼(我使用的是SSH框架搭建環境)
一、引入maven jar包
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</ artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>3.2.0</version>
</dependency>
二、編寫匯出的實體類(使用註解形式)
1、DeptUtil 類
@ExcelTarget("deptUtil")
public class DeptUtil {
@Excel(name = "部門編號", width = 30 , needMerge = true)
private Integer id;
@Excel(name = "部門名稱", width = 30 , needMerge = true)
private String deptName;
@ExcelCollection(name = "員工資訊")
private List<EmpUtil> emps;
....省略getter、setter方法
2、EmpUtil類
@ExcelTarget("empUtil")
public class EmpUtil{
@Excel(name = "序號", width = 30, isColumnHidden = true)
private Integer id;
@Excel(name = "員工姓名", width = 30, groupName = "基本資訊")
private String empName;
@Excel(name = "年齡", width = 30, type = 10, groupName = "基本資訊")
private Integer age;
@Excel(name = "入職時間", width = 30, groupName = "工作資訊", format = "yyyy/MM/dd HH:mm")
private Date hiredate;
@Excel(name = "薪酬", width = 30, type = 10, groupName = "工作資訊")
private BigDecimal salary;
....省略getter、setter方法
3、核心程式碼
public String export(){
Workbook workBook = null;
try {
List<DeptUtil> exportList = exportService.exportList();
System.err.println(JSONArray.toJSONString(exportList));
// 建立引數物件(用來設定excel得sheet得內容等資訊)
ExportParams deptExportParams = new ExportParams();
// 設定sheet得名稱
deptExportParams.setSheetName("員工報表1");
// 建立sheet1使用得map
Map<String, Object> deptExportMap = new HashMap<>();
// title的引數為ExportParams型別,目前僅僅在ExportParams中設定了sheetName
deptExportMap.put("title", deptExportParams);
// 模版匯出對應得實體型別
deptExportMap.put("entity", DeptUtil.class);
// sheet中要填充得資料
deptExportMap.put("data", exportList);
ExportParams empExportParams = new ExportParams();
empExportParams.setSheetName("員工報表2");
// 建立sheet2使用得map
Map<String, Object> empExportMap = new HashMap<>();
empExportMap.put("title", empExportParams);
empExportMap.put("entity", DeptUtil.class);
empExportMap.put("data", exportList);
// 將sheet1、sheet2、sheet3使用得map進行包裝
List<Map<String, Object>> sheetsList = new ArrayList<>();
sheetsList.add(deptExportMap);
sheetsList.add(empExportMap);
// 執行方法
workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
fileName = URLEncoder.encode("員工報表匯出", "UTF-8");
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
workBook.write(outputStream);
outputStream.flush();
byte[] byteArray = outputStream.toByteArray();
excelStream = new ByteArrayInputStream(byteArray,0,byteArray.length);
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if(workBook != null) {
try {
workBook.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "success";
}
三、效果展示
有一個問題就是如果sheet填充的資料來源是一樣的,那麼第二個sheet的內容就會為空
不過一般上要實現多sheet頁展示,內容應該是不一樣的,這裡只是作為實現效果展示,就採用了同一個資料獲取源。
以上就是所有內容了,如果想要學習更多可以查閱easyPoi的api(http://easypoi.mydoc.io/)
另外附上我的專案原始碼:https://github.com/wzqonly/easyPoi_export
喜歡的歡迎點贊O(∩_∩)O