Spring Boot 系列教程12-EasyPoi導出Excel下載


Java操作excel框架

Java Excel俗稱jxl,可以讀取Excel文件的內容、創建新的Excel文件、更新已經存在的Excel文件,現在基本沒有更新了

Apache POI是Apache基金組織Jakarta項目的子項目,它包括一系列的API,可以操作多種格式的Microsoft Office文件,通過這些API使Java更方便的操作Excel、Word等格式的Office文件

EasyPoi功能如同名字easy,主打的功能就是容易,讓一個沒見接觸過poi的人員 就可以方便的寫出Excel導出,Excel模板導出,Excel導入,Word模板導出,通過簡單的注解和模板 語言(熟悉的表達式語法),完成以前復雜的寫法

最終下載效果

這里寫圖片描述

項目圖片

這里寫圖片描述

UserController.download方法

  • 簡單的飛起
// 下載execl文檔
  @RequestMapping("/download")
  public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // 告訴瀏覽器用什么軟件可以打開此文件
    response.setHeader("content-Type", "application/vnd.ms-excel");
    // 下載文件的默認名稱
    response.setHeader("Content-Disposition", "attachment;filename=user.xls");
    List<User> list = userRepository.findAll();
    Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, list);
    workbook.write(response.getOutputStream());
  }

User.java

@Entity
@Table(name = "t_user")
@ExcelTarget("user")
public class User {
  @Id
  @GeneratedValue
  @Excel(name = "編號", orderNum = "1", mergeVertical = true, isImportField = "id")
  private Long id;
  @Excel(name = "姓名", orderNum = "2", mergeVertical = true, isImportField = "name")
  private String name;
  @Excel(name = "年齡", orderNum = "3", mergeVertical = true, isImportField = "age")
  private Integer age;

其他關聯項目

源碼地址

https://github.com/je-ge/spring-boot

如果覺得我的文章對您有幫助,請予以打賞。您的支持將鼓勵我繼續創作!謝謝!
微信打賞
支付寶打賞


免責聲明!

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



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