SpringBoot使用EasyExcel读取excel表格中的数据到数据库中

1,679 阅读1分钟

一、添加依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.1.1</version>
        </dependency>

二、创建实体类,对应excel表格

index = 0 表示excel表格的第一列

package com.sise.wangzhan.ucenter.entity.excel;

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

/**
 *  Copyright: Copyright (c) 2021 Asiainfo
 *  
 *  @ClassName: com.sise.wangzhan.ucenter.entity.excel.MemberData
 *  @Description: 该类的功能描述
 * <p>
 *  @version: v1.0.0
 *  @author:   wangzhan
 *  @date: 2021/1/20 17:32 
 * <p>
 *  Modification History:
 *  Date       Author    Version    Description
 * ----------------------------------------------------------
 *  2021/1/20    acer     v1.0.0      修改原因
 */
@Data
public class MemberData {

    @ExcelProperty(value = "昵称(学号/教师工号)", index = 0)
    private String nickname;

    @ExcelProperty(value = "真实姓名", index = 1)
    private String relName;

    @ExcelProperty(value = "手机号码", index = 2)
    private String mobile;

    @ExcelProperty(value = "密码(设置初始密码为学号/老师工号)", index = 3)
    private String password;

    @ExcelProperty(value = "系别", index = 4)
    private String department;

    @ExcelProperty(value = "年级", index = 5)
    private String grade;

    @ExcelProperty(value = "性别 1 女,2 男", index = 6)
    private Integer sex;

    @ExcelProperty(value = "年龄", index = 7)
    private Integer age;

}

三、创建读取操作的监听器

继承AnalysisEventListener,实现里面的两个方法invoke,doAfterAllAnalysed

四、调用