【easyexcel】读取excel文件

读取excel文件解析为实体对象列表

例:将如下文件解析为List<Object>

文件内容

 将该文件内容转换为List<User>

引入Maven依赖

 <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>easyexcel</artifactId>
      <version>1.1.2-beta5</version>
 </dependency>

User类

继承了BaseRowModel,字段上添加@ExcelProperty注解,属性value表示表头名,index表示列的顺序排序

package com.harara.model;

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

import java.util.Date;

/**
 * @author : harara
 * @version : 2.0
 * @date : 2020/6/9 15:32
 * 继承BaseRowModel
 */
@Data
public class User extends BaseRowModel{

    @ExcelProperty(value = "姓名", index = 0)
    private String name;

    @ExcelProperty(value = "年龄", index = 1)
    private String age;

    @ExcelProperty(value = "邮箱", index = 2)
    private String email;

    @ExcelProperty(value = "地址", index = 3)
    private String address;

    @ExcelProperty(value = "性别", index = 4)
    private String sax;

    @ExcelProperty(value = "高度", index = 5)
    private String heigh;

    @ExcelProperty(value = "备注", index = 6)
    private String last;

    @ExcelProperty(value = "生日",index = 7)
    private Date bitthday;

}

监听器

继承AnalysisEventListener

package com.harara.easyexcel.read;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import java.util.ArrayList;
import java.util.List;
/**
 * @author : harara
 * @version : 2.0
 * @date : 2020/6/10 9:25
 */
public class ExcelListener extends AnalysisEventListener {


    private List<Object> dataList = new ArrayList<Object>();
    /**
     * 每解析一行会回调invoke()方法
   * 通过AbalysisContext可以获取当前sheet,当前行等数据 *
@param object * @param context */ @Override public void invoke(Object object, AnalysisContext context) { dataList.add(object); } @Override public void doAfterAllAnalysed(AnalysisContext context) { //dosomething 整个excel解析结束后会执行这个方法 } public List<Object> getDataList(){ return dataList; } }

测试类

读取excel文件解析为实体对象列表

package com.harara.easyexcel.read;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.BaseRowModel;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.harara.model.User;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author : harara
 * @version : 2.0
 * @date : 2020/6/10 9:41
 */
public class ExcelReadTest {


    public <T extends BaseRowModel> List<T> read(String filename, Class<T> rowModel) throws Exception{

        ExcelListener excelListener = new ExcelListener();
        ExcelReader excelReader = getExcelReader(new File(filename),excelListener,true);
        if(excelReader == null){
            return new ArrayList();
        }
        for(Sheet sheet:excelReader.getSheets()){
            sheet.setClazz(rowModel);
            excelReader.read(sheet);
        }
        List<T> list = new ArrayList<>();
        for(Object obj:excelListener.getDataList()){
                list.add((T)obj);
        }
        return list;
    }


    /**
     *
     * @param file 文件
     * @param eventListener 用户监听器
     * @return
     */
    public static ExcelReader getExcelReader(File file,
                                             AnalysisEventListener eventListener) throws Exception{
        String fileName  = file.getName();
        if (fileName == null ) {
            throw new Exception("文件格式错误!");
        }
        if (!fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue()) && !fileName.toLowerCase().endsWith(ExcelTypeEnum.XLSX.getValue())) {
            throw new Exception("文件格式错误!");
        }
        InputStream inputStream = null;
        try{
            inputStream = new FileInputStream(file);
            if (fileName.toLowerCase().endsWith(ExcelTypeEnum.XLS.getValue())) {
                return new ExcelReader(inputStream, ExcelTypeEnum.XLS, null, eventListener, false);
            } else {
                return new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, eventListener, false);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }



    public static void main(String[] args) {
        ExcelReadTest excelReadTest = new ExcelReadTest();
        try {
            List<User> users = excelReadTest.read("excelByModel.xlsx", User.class);
            System.out.println(users);
            System.out.println("读取完成");
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}

参考地址

alibaba/easyexcel 框架使用  https://www.jianshu.com/p/3a64ade57bf2

JAVA使用easyexcel操作Excel https://blog.csdn.net/jianggujin/article/details/80200400

原文地址:https://www.cnblogs.com/kiko2014551511/p/13086949.html