SpringBoot實現(xiàn)導(dǎo)出復(fù)雜對象到Excel文件
在Spring Boot項目中導(dǎo)出復(fù)雜對象到Excel文件,可以利用Hutool或EasyExcel等庫來簡化操作。這里我們將詳細介紹如何使用Hutool和EasyExcel兩種方式來實現(xiàn)這一功能。
使用Hutool導(dǎo)出復(fù)雜對象到Excel
首先確保你的pom.xml中添加了Hutool的依賴:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version> <!-- 請根據(jù)實際情況選擇最新版本 -->
</dependency>
接下來是一個簡單的示例,展示如何導(dǎo)出一個包含復(fù)雜對象的列表到Excel文件。
示例代碼
假設(shè)我們有一個User類,它包含一個嵌套的Address對象。
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 {
// 模擬獲取用戶數(shù)據(jù)
List<User> users = getUsers();
// 創(chuàng)建ExcelWriter實例
ExcelWriter writer = ExcelUtil.getWriter(true); // true表示自動創(chuàng)建表頭
// 將復(fù)雜對象轉(zhuǎn)換為Map列表,方便寫入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());
// 寫入數(shù)據(jù)
writer.write(dataList, true);
// 設(shè)置響應(yīng)內(nèi)容類型和頭部信息
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String fileName = URLEncoder.encode("用戶列表", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
// 將輸出流寫入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ù)雜對象到Excel
EasyExcel是阿里巴巴開源的一個非常高效的Excel處理庫,特別適合處理大數(shù)據(jù)量的Excel文件。首先,在pom.xml中添加EasyExcel的依賴:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version> <!-- 請根據(jù)實際情況選擇最新版本 -->
</dependency>
接下來是一個使用EasyExcel導(dǎo)出復(fù)雜對象的例子。
示例代碼
假設(shè)我們?nèi)匀皇褂蒙厦嫣岬降?code>User和Address類。
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 {
// 模擬獲取用戶數(shù)據(jù)
List<User> users = getUsers();
// 設(shè)置響應(yīng)內(nèi)容類型和頭部信息
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String fileName = URLEncoder.encode("用戶列表", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
// 使用EasyExcel寫出數(shù)據(jù)到輸出流
EasyExcel.write(response.getOutputStream(), UserData.class)
.sheet("用戶信息")
.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ù)實體類
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方法
}
在這個例子中,我們定義了一個UserData類來映射User對象的數(shù)據(jù),并使用EasyExcel將這些數(shù)據(jù)寫入Excel文件。
通過上述方法,你可以輕松地在Spring Boot項目中導(dǎo)出復(fù)雜對象到Excel文件。無論是使用Hutool還是EasyExcel,都可以有效地簡化Excel處理的工作。
以上就是SpringBoot實現(xiàn)導(dǎo)出復(fù)雜對象到Excel文件的詳細內(nèi)容,更多關(guān)于SpringBoot導(dǎo)出復(fù)雜對象到Excel的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
如何在mapper文件中使用in("str1","str2")
這篇文章主要介紹了如何在mapper文件中使用in("str1","str2"),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
在idea中使用JaCoCo插件統(tǒng)計單元測試覆蓋率的實現(xiàn)
這篇文章主要介紹了在idea中使用JaCoCo插件統(tǒng)計單元測試覆蓋率的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
關(guān)于SpringBoot配置文件加載位置的優(yōu)先級
這篇文章主要介紹了關(guān)于SpringBoot配置文件加載位置的優(yōu)先級,我們也可以通過spring.config.location來改變默認的配置文件位置,項目打包好后,我們可以通過命令行的方式在啟動時指定配置文件的位置,需要的朋友可以參考下2023-10-10
Spring ApplicationListener的使用詳解
這篇文章主要介紹了Spring ApplicationListener的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
關(guān)于Springboot | @RequestBody 接收到的參數(shù)對象屬性為空的問題
這篇文章主要介紹了關(guān)于Springboot | @RequestBody 接收到的參數(shù)對象屬性為空的問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

