Export complex objects to Excel files in Spring Boot projects, and you can use libraries such as Hutool or EasyExcel to simplify operations. Here we will introduce in detail how to use Hutool and EasyExcel to implement this function.
Export complex objects to Excel using Hutool
First make sure yoursAdded Hutool dependencies:
<dependency> <groupId></groupId> <artifactId>hutool-all</artifactId> <version>5.8.10</version> <!-- Please select the latest version according to the actual situation --> </dependency>
Next is a simple example showing how to export a list of complex objects to an Excel file.
Sample code
Suppose we have oneUser
class, which contains a nestedAddress
Object.
import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; @RestController @RequestMapping("/api") public class UserController { @GetMapping("/exportUsers") public void exportUsers(HttpServletResponse response) throws IOException { // Simulate to obtain user data List<User> users = getUsers(); // Create an ExcelWriter instance ExcelWriter writer = (true); // true means automatic creation of table headers // Convert complex objects to Map lists for easy writing to Excel List<Map<String, Object>> dataList = ().map(user -> { Map<String, Object> row = new HashMap<>(); ("ID", ()); ("Name", ()); ("Mail", ()); ("age", ()); ("City", ().getCity()); ("street", ().getStreet()); return row; }).collect(()); // Write data (dataList, true); // Set the response content type and header information ("application/-excel;charset=utf-8"); String fileName = ("User List", "UTF-8"); ("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); // Write the output stream to the response ServletOutputStream out = (); (out, true); (); (); } private List<User> getUsers() { List<User> users = new ArrayList<>(); Address address = new Address("Beijing", "Zhongguancun Street"); (new User(1L, "Zhang San", "zhangsan@", 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) { = id; = name; = email; = age; = address; } // getter and setter methods} class Address { private String city; private String street; public Address(String city, String street) { = city; = street; } // getter and setter methods}
Export complex objects to Excel using EasyExcel
EasyExcel is a very efficient Excel processing library open source by Alibaba, which is especially suitable for processing large data volumes of Excel files. First, inAdd EasyExcel dependencies:
<dependency> <groupId></groupId> <artifactId>easyexcel</artifactId> <version>2.2.10</version> <!-- Please select the latest version according to the actual situation --> </dependency>
Next is an example of using EasyExcel to export complex objects.
Sample code
Assume we still use the above mentionedUser
andAddress
kind.
import ; import ; import ; import ; import ; import ; import ; import ; import ; @RestController @RequestMapping("/api") public class EasyExcelController { @GetMapping("/exportUsers") public void exportUsers(HttpServletResponse response) throws IOException { // Simulate to obtain user data List<User> users = getUsers(); // Set the response content type and header information ("application/-excel;charset=utf-8"); String fileName = ("User List", "UTF-8"); ("Content-Disposition", "attachment;filename=" + fileName + ".xlsx"); // Use EasyExcel to write data to output stream ((), ) .sheet("User Information") .doWrite(users); } private List<User> getUsers() { List<User> users = new ArrayList<>(); Address address = new Address("Beijing", "Zhongguancun Street"); (new User(1L, "Zhang San", "zhangsan@", 28, address)); return users; } } // Data Entity Classclass UserData { @("ID") private Long id; @("Name") private String name; @("Mail") private String email; @("age") private Integer age; @("City") private String city; @("street") private String street; // Constructor, getter and setter methods public UserData(User user) { = (); = (); = (); = (); = ().getCity(); = ().getStreet(); } // getter and setter methods}
In this example, we define aUserData
Classes to mapUser
The data of the object and write this data to the Excel file using EasyExcel.
With the above method, you can easily export complex objects to Excel files in Spring Boot projects. Whether using Hutool or EasyExcel, it can effectively simplify Excel processing.
The above is the detailed content of SpringBoot's implementation of exporting complex objects to Excel files. For more information about SpringBoot's exporting complex objects to Excel, please pay attention to my other related articles!