EasyExcel 读取文件

在开发过程中,有一个需求是上传excel,然后读取文件的内容,下面记录一下用easyexcel读取文件的代码。

1、引入jar包

<dependency>
    <groupId>com.alibabagroupId>
    <artifactId>easyexcelartifactId>
    <version>2.1.1version>
dependency>

2、方法层

2.1设计对应的实体类接收每一行

package com.example.shiro.entity.excel;

import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import lombok.Data;
import lombok.ToString;


@Data
@ToString
public class ExcelEntity extends BaseRowModel {
    /**
     * 第一列
     */
    @ExcelProperty(index = 0)
    private String column1;
    /**
     * 第二列
     */
    @ExcelProperty(index = 1)
    private String column2;

    /**
     * 第三列
     */
    @ExcelProperty(index = 2)
    private String column3;

    /**
     * 第四列
     */
    @ExcelProperty(index = 3)
    private String column4;

    /**
     * 第五列
     */
    @ExcelProperty(index = 4)
    private String column5;

}

2.2 读取到每一行数据的处理

package com.example.shiro.service.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.shiro.entity.excel.ExcelEntity;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class EasyExcelListener extends AnalysisEventListener<ExcelEntity> {
    List<ExcelEntity> list = new ArrayList<>();
    Map<String,Long> elementMap = new HashMap<>();
    Map<String,Long> eventNameMap = new HashMap<>();
    @Override
    public void invoke(ExcelEntity excelEntity, AnalysisContext analysisContext) {
        //do some business logic
        log.info("一行一行读取数据,可以设置批次");
        list.add(excelEntity);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        //do some business logic
        log.info("数据读取完毕");
    }

    public List<ExcelEntity> getDatas() {
        return list;
    }
}

2.3 方法入口

public List<ExcelEntity> easyExcelReader() {
        String readPath = "C:\\Users\\Desktop\\app\\test.xlsx";
        EasyExcelListener listener = new EasyExcelListener();
        ExcelReader excelReader = EasyExcelFactory.read(readPath,ExcelEntity.class, listener)
                .headRowNumber(1)
                .ignoreEmptyRow(false)//不忽略空行 默认是忽略空行
                .build();
        excelReader.readAll();
        return listener.getDatas();
    }

2.4 返回结果

[
        {
            "cellStyleMap":{

            },
            "column1":"Test",
            "column2":"ceshi6",
            "column3":"aa",
            "column4":"ceshi61",
            "column5":null
        },
        {
            "cellStyleMap":{

            },
            "column1":null,
            "column2":null,
            "column3":null,
            "column4":null,
            "column5":null
        },
        {
            "cellStyleMap":{

            },
            "column1":"Test",
            "column2":"ceshi6",
            "column3":"aa",
            "column4":"ceshi61",
            "column5":"excelClick13"
        }
    ]

你可能感兴趣的:(java)