SpringBoot實現導出復雜對象到Excel文件
在Spring Boot項目中導出復雜對象到Excel文件,可以利用Hutool或EasyExcel等庫來簡化操作。這里我們將詳細介紹如何使用Hutool和EasyExcel兩種方式來實現這一功能。
使用Hutool導出復雜對象到Excel
首先確保你的pom.xml中添加了Hutool的依賴:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version> <!-- 請根據實際情況選擇最新版本 -->
</dependency>
接下來是一個簡單的示例,展示如何導出一個包含復雜對象的列表到Excel文件。
示例代碼
假設我們有一個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 {
// 模擬獲取用戶數據
List<User> users = getUsers();
// 創(chuàng)建ExcelWriter實例
ExcelWriter writer = ExcelUtil.getWriter(true); // true表示自動創(chuàng)建表頭
// 將復雜對象轉換為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());
// 寫入數據
writer.write(dataList, true);
// 設置響應內容類型和頭部信息
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("北京", "中關村大街");
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導出復雜對象到Excel
EasyExcel是阿里巴巴開源的一個非常高效的Excel處理庫,特別適合處理大數據量的Excel文件。首先,在pom.xml中添加EasyExcel的依賴:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.10</version> <!-- 請根據實際情況選擇最新版本 -->
</dependency>
接下來是一個使用EasyExcel導出復雜對象的例子。
示例代碼
假設我們仍然使用上面提到的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 {
// 模擬獲取用戶數據
List<User> users = getUsers();
// 設置響應內容類型和頭部信息
response.setContentType("application/vnd.ms-excel;charset=utf-8");
String fileName = URLEncoder.encode("用戶列表", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
// 使用EasyExcel寫出數據到輸出流
EasyExcel.write(response.getOutputStream(), UserData.class)
.sheet("用戶信息")
.doWrite(users);
}
private List<User> getUsers() {
List<User> users = new ArrayList<>();
Address address = new Address("北京", "中關村大街");
users.add(new User(1L, "張三", "zhangsan@example.com", 28, address));
return users;
}
}
// 數據實體類
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;
// 構造函數、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對象的數據,并使用EasyExcel將這些數據寫入Excel文件。
通過上述方法,你可以輕松地在Spring Boot項目中導出復雜對象到Excel文件。無論是使用Hutool還是EasyExcel,都可以有效地簡化Excel處理的工作。
以上就是SpringBoot實現導出復雜對象到Excel文件的詳細內容,更多關于SpringBoot導出復雜對象到Excel的資料請關注腳本之家其它相關文章!
相關文章
如何在mapper文件中使用in("str1","str2")
這篇文章主要介紹了如何在mapper文件中使用in("str1","str2"),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring ApplicationListener的使用詳解
這篇文章主要介紹了Spring ApplicationListener的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
關于Springboot | @RequestBody 接收到的參數對象屬性為空的問題
這篇文章主要介紹了關于Springboot | @RequestBody 接收到的參數對象屬性為空的問題,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03

