easy額xcel 生成Excel表並返回瀏覽器彈出下載的簡單實現


第一步:添加依賴

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>1.0.2</version>
</dependency>

說明:阿里easyexcel導出excel文件的思路:

  1. 和管理后台普通的查詢列表一樣,設計Api需要具備按條件查詢的功能特性
  2. 按條件查詢出滿足條件的records,封裝成List集合
  3. 使用easyexcel構建的工具類通過字節流讀取,搭檔輸出流將數據寫入Excel

第二步:ExcelUtil工具類

import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.metadata.BaseRowModel; import com.alibaba.excel.metadata.Sheet; import com.alibaba.excel.support.ExcelTypeEnum; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.List; public class ExcelUtil { /** * 導出 Excel :一個 sheet,帶表頭 * * @param response HttpServletResponse * @param list 數據 list,每個元素為一個 BaseRowModel * @param fileName 導出的文件名 * @param sheetName 導入文件的 sheet 名 * @param model 映射實體類,Excel 模型 */
    public static void writeExcel(HttpServletResponse response, List<? extends BaseRowModel> list, String fileName, String sheetName, BaseRowModel model)throws Exception { ExcelWriter writer = new ExcelWriter(getOutputStream(fileName, response), ExcelTypeEnum.XLSX); Sheet sheet = new Sheet(1, 0, model.getClass()); sheet.setSheetName(sheetName); writer.write(list, sheet); writer.finish(); } /** * 導出文件時為Writer生成OutputStream * * @param fileName * @param response * @return */
    private static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws Exception { try { fileName = URLEncoder.encode(fileName, "UTF-8"); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf8"); response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xlsx"); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "no-store"); response.addHeader("Cache-Control", "max-age=0"); return response.getOutputStream(); } catch (IOException e) { throw new Exception("導出excel表格失敗!", e); } } }

 

第三步:封裝model對象

public class exportDataModel extends BaseRowModel { @ExcelProperty(value = { "列1", "列1" }, index = 0) private String 屬性1; @ExcelProperty(value = { "列2", "列2" }, index = 1) private String 屬性2; @ExcelProperty(value = { "列3", "列4" }, index = 2) private String 屬性3; @ExcelProperty(value = { "列3", "列5" }, index = 3) private String 屬性4; @ExcelProperty(value = { "列6", "列7" }, index = 4) private String 屬性5; @ExcelProperty(value = { "列6", "列8" }, index = 5) private String 屬性6;  

說明:作為映射實體類,需要繼承 BaseRowModel 類,通過 @ExcelProperty 注解與 index 變量可以標注成員變量所映射的列,同時不可缺少 setter 方法

本文主要使用到@ExcelProperty注解的2個屬性

  • value:用於指定Excel表頭名稱
  • index:用於指定表頭所在列的索引值,從0開始

第四部:Controller代碼演示

    try { String name = "演示導出模板.xlsx"; Date date = Calendar.getInstance().getTime(); SimpleDateFormat sdf_ymd = new SimpleDateFormat("yyyyMMddHHmmss"); String formatDate_ymd = sdf_ymd.format(date); // 設置文件名
            String fileName = formatDate_ymd + name; String sheetName = "數據展示"; // 按條件篩選records
            List<exportDataModel> list = this.getExportDataList(); // easyexcel工具類實現Excel文件導出
            ExcelUtil.writeExcel(response, list, fileName, sheetName, new exportDataModel()); } catch (Exception e) { e.printStackTrace(); }

說明:fileName,sheetName 分別是導出文件的文件名和 sheet 名,new exportDataModel() 為導出數據的映射實體對象,list 為導出數據。

第五步:效果展示

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM