SpringBoot realizes merging table headers to export data-EasyExcel application



insert image description here

EasyExcel is an open source Java tool library for processing Excel files. It provides an easy-to-use API to read, write and manipulate Excel files conveniently. The following are some common EasyExcel application scenarios:

Read Excel files: Use EasyExcel to easily read data in Excel files and convert them into Java objects or collections. You can specify the starting row and column to read, and you can also process data from different Sheets.

Write Excel files: EasyExcel allows Java objects or collections to be written into Excel files. You can set the title, style and other attributes of each column according to your needs, and at the same time support the writing operation of large amounts of data.

Complex data processing: EasyExcel provides a wealth of data processing functions, such as filtering, sorting, merging and other operations, making the processing of Excel data more convenient.

Import and export tools: With EasyExcel, you can quickly build complex import and export tools to import data from databases or other sources to Excel, or export data in Excel to other formats.

Data verification and conversion: EasyExcel supports verification and conversion operations on read Excel data, including basic data type verification, date format conversion, dictionary data conversion, etc., to ensure data accuracy and consistency.

Template export: By combining Excel templates with EasyExcel, you can generate Excel reports in custom formats. The placeholders in the template file can be replaced with specific data to generate the final Excel report.

EasyExcel is easy to use and has better performance when dealing with large amounts of data. You can refer to the official documentation or sample code of EasyExcel to learn more details about its usage and functions.


In the previous section, we realized the export of data, and the effect is as follows:
insert image description here

However, in our actual business, the data is often more complicated, and there will be cases of merging headers. So in this article, we will implement merging headers.

1. Build entity class

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class User implements Serializable {
    
    

    @ExcelProperty(value = {
    
    "用户基本信息","用户名"},index = 0)
    private String userName;

    @ExcelProperty(value = {
    
    "用户基本信息","年龄"},index = 1)
    private Integer age;

    @ExcelProperty(value = {
    
    "用户基本信息","地址"} ,index = 2)
    private String address;

    @ExcelProperty(value = {
    
    "用户基本信息","生日"},index = 3)
    //注意:日期格式注解由alibaba.excel提供
//    @DateTimeFormat("yyyy-MM-dd HH:mm")
    private Date birthday;
}

2. Export

public static void main(String[] args) {
    
    
  //组装数据
    ArrayList<User> users = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
    
    
        User user = new User();
        user.setAddress("西安" + i);
        user.setUserName("张三" + i);
        user.setBirthday(new Date());
        user.setAge(10 + i);
        users.add(user);
    }

    //不做任何注解处理时,表头名称与实体类属性名称一致
    EasyExcel.write("D:\\用户.xlsx", User.class).sheet("用户信息").doWrite(users);
}

The effect is as follows
insert image description here


3. Import

/**
 * excel数据格式必须与实体类定义一致,否则数据读取不到
 */
@Test
public void readExcel(){
    
    
    ArrayList<User> users = new ArrayList<>();
    //读取数据
    EasyExcel.read("C:\\用户.xls", User.class, new AnalysisEventListener<User>() {
    
    
        @Override
        public void invoke(User o, AnalysisContext analysisContext) {
    
    
            System.out.println(o);
            users.add(o);
        }
        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    
            System.out.println("完成。。。。");
        }
    }).sheet().doRead();
    System.out.println(users);
}

Summarize:

EasyExcel can use the method of merging cells to export Excel files with merging headers.

During the export process, the header data of each row is written first, and then the data row is written. Finally, define a merged cell range through CellRangeAddress, and merge the corresponding cells in the table header.

Note: The above sample code is for reference only, and you need to make appropriate modifications according to your own needs in actual use.


insert image description here

Guess you like

Origin blog.csdn.net/m0_60915009/article/details/131394164