Springboot集成easypoi實現excel多sheet導出

1.環境配置

<!--easypoi依賴,excel導入導出-->
<dependency>
    <groupId>cn.afterturn</groupId>
    <artifactId>easypoi-spring-boot-starter</artifactId>
    <version>4.3.0</version>
</dependency>

2.先來定義兩個導出數據的實體類
ExcelUser.java

public class Dept{

  @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方法

ExcelLog.java

public class ExcelLog{

  @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;
  
  @Excel(name = "頭像", type = 2, width = 30.0, height = 30.0, imageType = 1)
  private String image;

	....省略getter、setter方法

3.具體實現代碼如下:
UserServiceImpl.java

/**
 * excel多sheet導出
 */
@Override
public void exportSheet(HttpServletResponse response) {
	//功能描述:把同一個表格多個sheet測試結果重新輸出,
    Workbook workBook = null;
    try {
        // 創建參數對象(用來設定excel的sheet1內容等信息)
        ExportParams userExportParams = new ExportParams();
        // 設置sheet得名稱
        userExportParams.setSheetName("用戶表");
        // 設置sheet表頭名稱
        userExportParams.setTitle("用戶列表");
        // 創建sheet1使用得map
        Map<String, Object> userExportMap = new HashMap<>();
        // title的參數爲ExportParams類型,目前僅僅在ExportParams中設置了sheetName
        userExportMap.put("title", userExportParams);
        // 模版導出對應得實體類型
        userExportMap.put("entity", Dept.class);
        //轉成導出vo類型
        List<ExportExcelUser> users = this.changeType(this.list());
        // sheet1中要填充得數據
        userExportMap.put("data", users);
        //---------------------------------------
        // 創建參數對象(用來設定excel的sheet2內容等信息)
        ExportParams logInfoExportParams = new ExportParams();
        logInfoExportParams.setTitle("日誌列表");
        logInfoExportParams.setSheetName("日誌表");
        // 創建sheet2使用的map
        Map<String, Object> logInfoExportMap = new HashMap<>();
        logInfoExportMap.put("title", logInfoExportParams);
        logInfoExportMap.put("entity", ExcelLog.class);
        
        //查詢log數據 
        List<LogInfo> logInfoEntitys = logInfoMapper.selectList(new QueryWrapper<>());
        //轉成導出vo類型
        List<ExportExcelLog> logInfos = this.changeInfoType(logInfoEntitys);
        
        // sheet2中要填充得數據
        logInfoExportMap.put("data", logInfos);
        //---------------------------------------
        // 將sheet1、sheet2使用得map進行包裝
        List<Map<String, Object>> sheetsList = new ArrayList<>();
        //後續增加sheet組,則後面繼續追加即可;
        sheetsList.add(userExportMap);
        sheetsList.add(logInfoExportMap);
        // 執行方法
        workBook = ExcelExportUtil.exportExcel(sheetsList, ExcelType.HSSF);
        //設置編碼格式
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        //設置內容類型
        response.setContentType("application/octet-stream");
        //設置頭及文件命名。
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用戶及操作日誌導出.xls", StandardCharsets.UTF_8.name()));
        //寫出流
        workBook.write(response.getOutputStream());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (workBook != null) {
            try {
                //強行關流
                workBook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

導出效果:
在這裏插入圖片描述

通過查看源碼 ExcelExportUtil.exportExcel()方法
在這裏插入圖片描述

可得知,進行map.put(),其中的key必須是"title",“entity"和"data”。
已經可以在源碼方法可進行查看,所以設置參數類型等,都得按照這仨個key_name 進行put賦值。

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章