SpringBoot實(shí)現(xiàn)導(dǎo)出復(fù)雜對(duì)象到Excel文件
在Spring Boot項(xiàng)目中導(dǎo)出復(fù)雜對(duì)象到Excel文件,可以利用Hutool或EasyExcel等庫(kù)來(lái)簡(jiǎn)化操作。這里我們將詳細(xì)介紹如何使用Hutool和EasyExcel兩種方式來(lái)實(shí)現(xiàn)這一功能。
使用Hutool導(dǎo)出復(fù)雜對(duì)象到Excel
首先確保你的pom.xml
中添加了Hutool的依賴(lài):
<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.10</version> <!-- 請(qǐng)根據(jù)實(shí)際情況選擇最新版本 --> </dependency>
接下來(lái)是一個(gè)簡(jiǎn)單的示例,展示如何導(dǎo)出一個(gè)包含復(fù)雜對(duì)象的列表到Excel文件。
示例代碼
假設(shè)我們有一個(gè)User
類(lèi),它包含一個(gè)嵌套的Address
對(duì)象。
import cn.hutool.poi.excel.ExcelUtil; import cn.hutool.poi.excel.ExcelWriter; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @RestController @RequestMapping("/api") public class UserController { @GetMapping("/exportUsers") public void exportUsers(HttpServletResponse response) throws IOException { // 模擬獲取用戶(hù)數(shù)據(jù) List<User> users = getUsers(); // 創(chuàng)建ExcelWriter實(shí)例 ExcelWriter writer = ExcelUtil.getWriter(true); // true表示自動(dòng)創(chuàng)建表頭 // 將復(fù)雜對(duì)象轉(zhuǎn)換為Map列表,方便寫(xiě)入Excel List<Map<String, Object>> dataList = users.stream().map(user -> { Map<String, Object> row = new HashMap<>(); row.put("ID", user.getId()); row.put("姓名", user.getName()); row.put("郵箱", user.getEmail()); row.put("年齡", user.getAge()); row.put("城市", user.getAddress().getCity()); row.put("街道", user.getAddress().getStreet()); return row; }).collect(Collectors.toList()); // 寫(xiě)入數(shù)據(jù) writer.write(dataList, true); // 設(shè)置響應(yīng)內(nèi)容類(lèi)型和頭部信息 response.setContentType("application/vnd.ms-excel;charset=utf-8"); String fileName = URLEncoder.encode("用戶(hù)列表", "UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); // 將輸出流寫(xiě)入response ServletOutputStream out = response.getOutputStream(); writer.flush(out, true); out.close(); writer.close(); } private List<User> getUsers() { List<User> users = new ArrayList<>(); Address address = new Address("北京", "中關(guān)村大街"); users.add(new User(1L, "張三", "zhangsan@example.com", 28, address)); return users; } } class User { private Long id; private String name; private String email; private Integer age; private Address address; public User(Long id, String name, String email, Integer age, Address address) { this.id = id; this.name = name; this.email = email; this.age = age; this.address = address; } // getter和setter方法 } class Address { private String city; private String street; public Address(String city, String street) { this.city = city; this.street = street; } // getter和setter方法 }
使用EasyExcel導(dǎo)出復(fù)雜對(duì)象到Excel
EasyExcel是阿里巴巴開(kāi)源的一個(gè)非常高效的Excel處理庫(kù),特別適合處理大數(shù)據(jù)量的Excel文件。首先,在pom.xml
中添加EasyExcel的依賴(lài):
<dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> <!-- 請(qǐng)根據(jù)實(shí)際情況選擇最新版本 --> </dependency>
接下來(lái)是一個(gè)使用EasyExcel導(dǎo)出復(fù)雜對(duì)象的例子。
示例代碼
假設(shè)我們?nèi)匀皇褂蒙厦嫣岬降?code>User和Address
類(lèi)。
import com.alibaba.excel.EasyExcel; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; @RestController @RequestMapping("/api") public class EasyExcelController { @GetMapping("/exportUsers") public void exportUsers(HttpServletResponse response) throws IOException { // 模擬獲取用戶(hù)數(shù)據(jù) List<User> users = getUsers(); // 設(shè)置響應(yīng)內(nèi)容類(lèi)型和頭部信息 response.setContentType("application/vnd.ms-excel;charset=utf-8"); String fileName = URLEncoder.encode("用戶(hù)列表", "UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); // 使用EasyExcel寫(xiě)出數(shù)據(jù)到輸出流 EasyExcel.write(response.getOutputStream(), UserData.class) .sheet("用戶(hù)信息") .doWrite(users); } private List<User> getUsers() { List<User> users = new ArrayList<>(); Address address = new Address("北京", "中關(guān)村大街"); users.add(new User(1L, "張三", "zhangsan@example.com", 28, address)); return users; } } // 數(shù)據(jù)實(shí)體類(lèi) class UserData { @com.alibaba.excel.annotation.ExcelProperty("ID") private Long id; @com.alibaba.excel.annotation.ExcelProperty("姓名") private String name; @com.alibaba.excel.annotation.ExcelProperty("郵箱") private String email; @com.alibaba.excel.annotation.ExcelProperty("年齡") private Integer age; @com.alibaba.excel.annotation.ExcelProperty("城市") private String city; @com.alibaba.excel.annotation.ExcelProperty("街道") private String street; // 構(gòu)造函數(shù)、getter和setter方法 public UserData(User user) { this.id = user.getId(); this.name = user.getName(); this.email = user.getEmail(); this.age = user.getAge(); this.city = user.getAddress().getCity(); this.street = user.getAddress().getStreet(); } // getter和setter方法 }
在這個(gè)例子中,我們定義了一個(gè)UserData
類(lèi)來(lái)映射User
對(duì)象的數(shù)據(jù),并使用EasyExcel將這些數(shù)據(jù)寫(xiě)入Excel文件。
通過(guò)上述方法,你可以輕松地在Spring Boot項(xiàng)目中導(dǎo)出復(fù)雜對(duì)象到Excel文件。無(wú)論是使用Hutool還是EasyExcel,都可以有效地簡(jiǎn)化Excel處理的工作。
以上就是SpringBoot實(shí)現(xiàn)導(dǎo)出復(fù)雜對(duì)象到Excel文件的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot導(dǎo)出復(fù)雜對(duì)象到Excel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java8 實(shí)現(xiàn)map以value值排序操作
這篇文章主要介紹了java8 實(shí)現(xiàn)map以value值排序操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12如何在mapper文件中使用in("str1","str2")
這篇文章主要介紹了如何在mapper文件中使用in("str1","str2"),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01在idea中使用JaCoCo插件統(tǒng)計(jì)單元測(cè)試覆蓋率的實(shí)現(xiàn)
這篇文章主要介紹了在idea中使用JaCoCo插件統(tǒng)計(jì)單元測(cè)試覆蓋率的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01關(guān)于SpringBoot配置文件加載位置的優(yōu)先級(jí)
這篇文章主要介紹了關(guān)于SpringBoot配置文件加載位置的優(yōu)先級(jí),我們也可以通過(guò)spring.config.location來(lái)改變默認(rèn)的配置文件位置,項(xiàng)目打包好后,我們可以通過(guò)命令行的方式在啟動(dòng)時(shí)指定配置文件的位置,需要的朋友可以參考下2023-10-10Spring ApplicationListener的使用詳解
這篇文章主要介紹了Spring ApplicationListener的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06關(guān)于Springboot | @RequestBody 接收到的參數(shù)對(duì)象屬性為空的問(wèn)題
這篇文章主要介紹了關(guān)于Springboot | @RequestBody 接收到的參數(shù)對(duì)象屬性為空的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03