springboot實現(xiàn)excel表格導出幾種常見方法
更新時間:2023年11月29日 14:42:10 作者:就叫飛六吧
在日常的開發(fā)中避免不了操作Excel,下面這篇文章主要給大家介紹了關于springboot實現(xiàn)excel表格導出的幾種常見方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
簡介
在Spring Boot中,實現(xiàn)Excel表格導出的方式有很多種,以下是幾種常見的方法:
- 使用Apache POI:Apache POI是一個開源的Java API,用于處理Microsoft Office文檔格式,包括Excel電子表格。在Spring Boot中,可以使用Apache POI創(chuàng)建Excel文檔,并將其寫入HTTP響應中,以實現(xiàn)Excel表格的導出。
- 使用EasyPOI:EasyPOI是一個開源的Java API,用于處理Excel電子表格。它基于Apache POI和Jxls開發(fā),提供了更加簡單易用的API,可以幫助我們快速實現(xiàn)Excel表格的導出。
- 使用Jxls:Jxls是一個用于生成Excel報表的Java庫。在Spring Boot中,可以使用Jxls創(chuàng)建Excel文檔,并將其寫入HTTP響應中,以實現(xiàn)Excel表格的導出。
- 使用第三方庫:還有其他一些第三方的Java庫可以用于生成Excel電子表格,例如Aspose.Cells、JExcelApi等,它們也可以在Spring Boot中使用,實現(xiàn)Excel表格的導出。
需要注意的是,無論使用哪種方法,都需要將Excel文檔寫入HTTP響應中,并設置正確的Content-Type和Content-Disposition頭信息,以確保瀏覽器能夠正確地識別Excel文檔并下載它。
一、Apache POI
- maven依賴坐標
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency>
@RestController public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) throws Exception { // 創(chuàng)建Excel文檔 XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Sheet1"); // 創(chuàng)建表頭 XSSFRow header = sheet.createRow(0); header.createCell(0).setCellValue("姓名"); header.createCell(1).setCellValue("年齡"); header.createCell(2).setCellValue("性別"); // 填充數據 List<User> users = getUserList(); int rowIndex = 1; for (User user : users) { XSSFRow row = sheet.createRow(rowIndex++); row.createCell(0).setCellValue(user.getName()); row.createCell(1).setCellValue(user.getAge()); row.createCell(2).setCellValue(user.getGender()); } // 設置響應頭信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=users.xlsx"); // 將Excel文檔寫入響應流中 ServletOutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } // 模擬獲取用戶數據 private List<User> getUserList() { List<User> users = new ArrayList<>(); users.add(new User("張三", 25, "男")); users.add(new User("李四", 30, "女")); users.add(new User("王五", 28, "男")); return users; } // 用戶實體類 private static class User { private String name; private int age; private String gender; public User(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } } }
二、Easy POI
- maven依賴坐標
<dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-base</artifactId> <version>4.2.0</version> </dependency> <dependency> <groupId>cn.afterturn</groupId> <artifactId>easypoi-web</artifactId> <version>4.2.0</version> </dependency>
@RestController public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) throws Exception { // 創(chuàng)建Excel文檔 Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用戶列表", "用戶信息"), User.class, getUserList()); // 設置響應頭信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=users.xlsx"); // 將Excel文檔寫入響應流中 ServletOutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } // 模擬獲取用戶數據 private List<User> getUserList() { List<User> users = new ArrayList<>(); users.add(new User("張三", 25, "男")); users.add(new User("李四", 30, "女")); users.add(new User("王五", 28, "男")); return users; } // 用戶實體類 private static class User { @Excel(name = "姓名", orderNum = "0") private String name; @Excel(name = "年齡", orderNum = "1") private int age; @Excel(name = "性別", orderNum = "2") private String gender; public User(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } } }
三、Jxls
- maven依賴坐標
<dependency> <groupId>org.jxls</groupId> <artifactId>jxls</artifactId> <version>2.14.0</version> </dependency> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-poi</artifactId> <version>2.14.0</version> </dependency>
@RestController public class ExcelController { @GetMapping("/export") public void exportExcel(HttpServletResponse response) throws Exception { // 加載Excel模板 InputStream inputStream = getClass().getResourceAsStream("/templates/user_template.xlsx"); Workbook workbook = WorkbookFactory.create(inputStream); // 填充數據 List<User> users = getUserList(); Map<String, Object> model = new HashMap<>(); model.put("users", users); JxlsHelper.getInstance().processTemplate(model, workbook.getSheetAt(0)); // 設置響應頭信息 response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=users.xlsx"); // 將Excel文檔寫入響應流中 ServletOutputStream outputStream = response.getOutputStream(); workbook.write(outputStream); outputStream.flush(); outputStream.close(); } // 模擬獲取用戶數據 private List<User> getUserList() { List<User> users = new ArrayList<>(); users.add(new User("張三", 25, "男")); users.add(new User("李四", 30, "女")); users.add(new User("王五", 28, "男")); return users; } // 用戶實體類 private static class User { private String name; private int age; private String gender; public User(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } } }
總結
到此這篇關于springboot實現(xiàn)excel表格導出幾種常見方法的文章就介紹到這了,更多相關springboot excel表格導出內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java 使用 HttpClient 發(fā)送 GET請求和 POST請求
本文主要介紹了Java 使用 HttpClient 發(fā)送 GET請求和 POST請求,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08