Introduction to Easyexcel and writing and reading operations

1. Background

As a back-end staff who often performs data analysis, you are inevitably faced with various reports. In your daily work, you often need to process large amounts of data and perform various complex calculations and analyses. As an important tool, Excel also plays a vital role in data processing and analysis. However, Excel's cumbersome operation and low efficiency also restrict our work efficiency. In order to solve this problem, we can use EasyExcel, an excellent Java class library, to optimize the reading and writing of Excel data and improve our work efficiency.

2. Introduction

  • The more famous frameworks for parsing and generating Excel in the Java field include Apache poi, jxl, etc. But they all have a serious problem: they consume a lot of memory. If the amount of concurrency in your system is not large, it may be okay, but once the concurrency increases, it will definitely cause OOM or frequent full gc of the JVM.
  • EasyExcel is an open source excel processing framework from Alibaba, which is famous for its simplicity of use and memory saving. The main reason why EasyExcel can greatly reduce the memory usage is that when parsing Excel, the file data is not loaded into the memory all at once. Instead, the data is read line by line from the disk and parsed one by one.
  • EasyExcel adopts a row-by-row parsing mode and notifies processing of the parsing results of a row in observer mode (AnalysisEventListener)

Official website: https://easyexcel.opensource.alibaba.com
github address: https://github.com/alibaba/easyexcel
gitee address: https://gitee.com/easyexcel/easyexcel

3. Introduce dependencies

<!-- xls格式excel依赖包 -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.17</version>
</dependency>
<!--xlsx格式excel依赖包-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>
<!-- easyexcel依赖包 -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>easyexcel</artifactId>
    <version>2.1.1</version>
</dependency>

4. Code implementation

1.Create entity class

The attribute content of this entity class must be bound to the header field of Excel. You can add @ExcelProperty to the attribute to specify the corresponding header content.

public class UserData {
    
    
    @ExcelProperty("用户id")
    private Integer uid;
    @ExcelProperty("用户名")
    private String userName;
    @ExcelProperty("性别")
    private String sex;
    @ExcelProperty("年龄")
    private Integer age;

    public Integer getUid() {
    
    
        return uid;
    }

    public void setUid(Integer uid) {
    
    
        this.uid = uid;
    }

    public String getUserName() {
    
    
        return userName;
    }

    public void setUserName(String userName) {
    
    
        this.userName = userName;
    }

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }
}

2. Write to excel operation

public class ExcelWriteDemo {
    
    
    public static void main(String[] args) {
    
    
        List<UserData> list = new ArrayList<>();
        for(int i = 0;i<100;i++){
    
    
            UserData userData = new UserData();
            userData.setUid(i);
            userData.setUserName("excelwriteTest"+i);
            userData.setSex("男");
            userData.setAge(20+i);
            list.add(userData);
        }

        String fileName = "D:\\excelTest\\20230729.xls";
        EasyExcel.write(fileName,UserData.class).sheet("用户信息").doWrite(list);
    }

}

The file after execution is as follows:

11:58:22.004 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Begin to Initialization 'WriteContextImpl'
11:58:22.037 [main] DEBUG com.alibaba.excel.metadata.property.ExcelHeadProperty - The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is CLASS
11:58:22.113 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - CurrentConfiguration is writeWorkbookHolder
11:58:22.433 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Initialization 'WriteContextImpl' complete
11:58:22.434 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Sheet number is null
11:58:22.435 [main] DEBUG com.alibaba.excel.metadata.property.ExcelHeadProperty - The initialization sheet/table 'ExcelHeadProperty' is complete , head kind is CLASS
11:58:22.435 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - CurrentConfiguration is writeSheetHolder
11:58:22.436 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Can not find sheet:0 ,now create it
11:58:22.854 [main] DEBUG com.alibaba.excel.context.WriteContextImpl - Finished write.

Insert image description here

3. Read file operation

3.1 Specify the corresponding index in excel

For read operations in excel, you need to modify the annotation to specify the index of which column in excel the attribute corresponds to.

public class UserData {
    
    
    @ExcelProperty(value = "用户id",index = 0)
    private Integer uid;
    @ExcelProperty(value = "用户名",index = 1)
    private String userName;
    @ExcelProperty(value = "性别",index = 2)
    private String sex;
    @ExcelProperty(value = "年龄",index = 3)
    private Integer age;

    public Integer getUid() {
    
    
        return uid;
    }

    public void setUid(Integer uid) {
    
    
        this.uid = uid;
    }

    public String getUserName() {
    
    
        return userName;
    }

    public void setUserName(String userName) {
    
    
        this.userName = userName;
    }

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

    public void setAge(Integer age) {
    
    
        this.age = age;
    }
}

3.2 Set up listeners

The read operation requires setting up a listener, the purpose is to read the data in the file line by line.

public class ExcelLisenner extends AnalysisEventListener<UserData> {
    
    
    // 一行一行的读取内容,从第二行开始
    @Override
    public void invoke(UserData userData, AnalysisContext analysisContext) {
    
    
        System.out.println(userData.toString());
        System.out.println("读取一行,执行操作");
    }


    // 读取之后执行的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    
        System.out.println("读取之后执行的操作");
    }

    // 读取第一行表头信息
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    
    
        System.out.println("表头信息:" + headMap);
    }
}

3.3 Perform read operations

public class ExcelReadDemo {
    
    
    public static void main(String[] args) {
    
    
        String fileName = "D:\\excelTest\\20230729.xls";
        EasyExcel.read(fileName,UserData.class,new ExcelLisenner()).sheet().doRead();
    }
}

The execution results are as follows:

表头信息:{
    
    0=用户id, 1=用户名, 2=性别, 3=年龄}
UserData{
    
    uid=0, userName='excelwriteTest0', sex='男', age=20}
读取一行,执行操作
UserData{
    
    uid=1, userName='excelwriteTest1', sex='男', age=21}
读取一行,执行操作
UserData{
    
    uid=2, userName='excelwriteTest2', sex='男', age=22}
读取一行,执行操作
UserData{
    
    uid=3, userName='excelwriteTest3', sex='男', age=23}
读取一行,执行操作
UserData{
    
    uid=4, userName='excelwriteTest4', sex='男', age=24}
读取一行,执行操作
UserData{
    
    uid=5, userName='excelwriteTest5', sex='男', age=25}
读取一行,执行操作
UserData{
    
    uid=6, userName='excelwriteTest6', sex='男', age=26}
读取一行,执行操作
UserData{
    
    uid=7, userName='excelwriteTest7', sex='男', age=27}
读取一行,执行操作
UserData{
    
    uid=8, userName='excelwriteTest8', sex='男', age=28}
读取一行,执行操作
UserData{
    
    uid=9, userName='excelwriteTest9', sex='男', age=29}
读取一行,执行操作
UserData{
    
    uid=10, userName='excelwriteTest10', sex='男', age=30}
读取一行,执行操作
UserData{
    
    uid=11, userName='excelwriteTest11', sex='男', age=31}
读取一行,执行操作
UserData{
    
    uid=12, userName='excelwriteTest12', sex='男', age=32}
读取一行,执行操作
UserData{
    
    uid=13, userName='excelwriteTest13', sex='男', age=33}
读取一行,执行操作
UserData{
    
    uid=14, userName='excelwriteTest14', sex='男', age=34}
读取一行,执行操作
UserData{
    
    uid=15, userName='excelwriteTest15', sex='男', age=35}
读取一行,执行操作
UserData{
    
    uid=16, userName='excelwriteTest16', sex='男', age=36}
读取一行,执行操作
UserData{
    
    uid=17, userName='excelwriteTest17', sex='男', age=37}
读取一行,执行操作
UserData{
    
    uid=18, userName='excelwriteTest18', sex='男', age=38}
读取一行,执行操作
UserData{
    
    uid=19, userName='excelwriteTest19', sex='男', age=39}
读取一行,执行操作
UserData{
    
    uid=20, userName='excelwriteTest20', sex='男', age=40}
读取一行,执行操作

读取之后执行的操作

Guess you like

Origin blog.csdn.net/m0_37899908/article/details/131991332