EasyExcel usage and steps

1. Import dependencies (version 3.1.0+ does not require poi dependencies)

<!--    easyExcel-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>3.1.1</version>
        </dependency>

2. Write data

1. Create the entity class to be written (@ExcelProperty corresponds to the field name of the first row of excel)

               

package com.easyexcel.excel; 

import com.alibaba.excel.annotation.ExcelProperty; 
import lombok.Data; 

@Data 
public class Demo { 

    @ExcelProperty("Student Number") 
    private Integer sno; 

    @ExcelProperty("Student Name") 
    private String name; 
}

2. Start writing data

The first is the file name and address filename()

Sheet name sheet() at the bottom of excel

Collection doWrite() for writing

Create the corresponding collection, then assign the value and write the content into

package com.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.easyexcel.excel.Demo;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
class EasyExcelApplicationTests {

    @Test
    void contextLoads() {
        List<Demo> list=new ArrayList<>();
        for(int i=0;i<10;i++){
            Demo demo=new Demo();
            demo.setSno(i);
            demo.setSname("data"+i);
            list.add(demo);
        }
        String filename="D:/桌面/test.xlsx";
        EasyExcel.write(filename, Demo.class).sheet("学生列表").doWrite(list);
    }

}

3. Operation results

 The effect is as follows:

 3. Read data

1. Create an entity class and add an index field to @ExcelProperty to indicate the column.

@Data 
public class Demo { 

    @ExcelProperty(value = "Student Number",index = 0) 
    private Integer sno; 

    @ExcelProperty(value = "Student Name",index = 1) 
    private String sname; 
}

2. Create an Excel listener

 Inherit the AnalysisEventListener class, implement the invoke and doAfterAllAnalysed methods, and implement the invokeHeadMap method, which is available in the right-click-generate-implementation method.

Among them, invoke is to read line by line.

doAfterAllAnalysed is the operation after reading

invokeHeadMap is to read the table header

package com.easyexcel.ExcelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.easyexcel.excel.Demo;

import java.util.Map;

public class ExcelListener extends AnalysisEventListener<Demo> {
    /**
     * 一行一行读
     * @param demo
     * @param analysisContext
     */

    @Override
    public void invoke(Demo demo, AnalysisContext analysisContext) {
        System.out.println("---"+demo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        System.out.println("表头="+headMap);
    }
}

3. Implement the read operation

First create the path filename of the file to be read

String filename="D:/Desktop/test.xlsx";

 Among them, EasyExcel's read has one more ExcelListener than write for listening, and the content is displayed in the listening class for reading.

EasyExcel.read(filename,Demo.class,new ExcelListener()).sheet().doRead();

 4.The results are as follows:

 The first is the excel content:

Then the result of reading 

 

Guess you like

Origin blog.csdn.net/weixin_55127182/article/details/127584518