Java implements easypoi to import data

1 introduce dependencies

<!--easypoi的依赖-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.2.0</version>
        </dependency>

2 modify the entity class

package *;

import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

import java.util.List;

@Data
public class Member {



    //姓名
    @Excel(name = "REAL_NAME")
    private String name;

    //电话
    @Excel(name = "MOBILE")
    private String phone;

    //邮件
    @Excel(name = "EMAIL")
    private String email;

    //邮件
    private String selfEmail;

    //账号
    @Excel(name = "PRACCT_NAME")
    private String fourUserName;


    //新增默认状态
    private String useStatus="1";

    //接收部门
    @Excel(name = "ORG_NAME")
    private String orgNAME;

    private String wbRoleId;


}

3 business code

  public Result membersBulkLoad(MultipartFile file) throws Exception {
        // 1.解析文档需要用到ImportParams 对象
        final ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);

        // 2.使用ExcelImportUtil工具类获取Excel表格中的内容,并转为List<T>,实际情况下我们需要知道上传的表格是什么类型的
        // *.因为所有的pojo都是按照用户需求创建的,拥有模板,上传的数据必须符合这些模板
        // *.那么实际开发中,可以通过访问的url判断本次上传的文件是属于什么类型的pojo
        final List<Member> students = ExcelImportUtil.importExcel(
                file.getInputStream(),
                Member.class,
                importParams
        );

        // 3.写入数据库,其中如果数据格式不符合,在上一步转换的时候会报错
//        this.studentService.saveAll(students);
        System.out.println(students);
        return Result.success(students);
    }

4excel document, the field name should correspond to the entity class

 

Personally, I feel that this is really easier to use than poi. When receiving poi, numbers are numbers, and strings are string types. Even if the string has numbers, it can be received with a string.

Guess you like

Origin blog.csdn.net/qq_40609490/article/details/126649494