SpringBoot implements export data-EasyExcel export data



insert image description here

1. Introduction to EasyExcel

The official website of EasyExcel introduces that most of the traditional operations of Excel are operated by Apache POI, but the POI
framework is not perfect, the use process is very cumbersome and has many defects:
dynamic operation of Excel is very cumbersome, and it is difficult for novices to do it in a short time. Get started from within;
reading and writing requires a large amount of memory, and memory overflow problems (OOM) are prone to occur when the amount of data is large; based on the above reasons, Alibaba has open sourced an easy-to-use and relatively memory-saving Excel operating framework: EasyExcel
Notice: The underlying layer of easyExcel is implemented using POI;

Official website address: https://www.yuque.com/easyexcel/doc/easyexcel


2. Export

2.1 Introducing dependencies

<!--引入easyexcel-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>3.0.4</version>
</dependency>

2.2 Build a test 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;
    
}

export code

    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);
    }

export effect
insert image description here


3. Set cell size

Add the following annotations to the class

@HeadRowHeight(value = 35) // 表头行高
@ContentRowHeight(value = 25) // 内容行高
@ColumnWidth(value = 50) // 列宽

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@HeadRowHeight(value = 35) // 表头行高
@ContentRowHeight(value = 25) // 内容行高
@ColumnWidth(value = 50) // 列宽
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;
}

The effect is as follows:
insert image description here

insert image description here

Guess you like

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