Java(18):easyExcel如何使用实体映射关系直接读写Excel数据

本文使用的是阿里巴巴的easyexcel工具类,github地址:https://github.com/alibaba/easyexcel

9.1.环境配置

Maven引用

        <!-- alibaba easyexcel依赖-->

        <dependency>

            <groupId>com.alibaba</groupId>

            <artifactId>easyexcel</artifactId>

            <version>2.2.9</version>

        </dependency>

9.2定义实体类

         我们首先需要定义实体类,读写 Excel 时,有时需要表头,如果需要表头,我们就可以在相应的实体类中加入 @ExcelProperty(value = "id", order = 0/1/2/3) 注解。其中 value 代表在导出 Excel 时,该字段对应的表头名称;index 代表该字段对应的表头位置。

实体类示例:

package com.ciphergateway.common.utils;

//import lombok.Data;

import com.alibaba.excel.annotation.ExcelProperty;

import java.io.Serializable;

import java.util.Date;

public class SDKData implements Serializable {

    @ExcelProperty("编号")

    private Integer id;

    @ExcelProperty("名字")

    private String name;

    @ExcelProperty("薪水")

    private Double salary;

    @ExcelProperty("生日")

    private Date birthday;

    public void setId(Integer id) {

        this.id=id;

    }

    public void setName(String name) {

        this.name=name;

    }

    public void setSalary(Double salary) {

        this.salary=salary;

    }

    public void setBirthday(Date birthday) {

        this.birthday=birthday;

    }

    public Integer getId() {

        return id;

    }

    public String getName() {

        return name;

    }

    public Double getSalary() {

        return salary;

    }

    public Date getBirthday() {

        return birthday;

    }

}

9.3先执行写数据操作,示例

    public static  List<SDKData> getData() {

        List<SDKData> lists = new ArrayList<>();

        for(int i = 0; i <= 10; i++) {

            SDKData sdkdata = new SDKData();

            sdkdata.setId(i+1);

            sdkdata.setName("张三" + i);

            sdkdata.setSalary(2000.00D);

            sdkdata.setBirthday(new Date());

            lists.add(sdkdata);

        }

        return lists;

    }

    public  static void writeExcel() {

        EasyExcel.write("学生表.xlsx", SDKData.class)

                .sheet()

                .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())

                .doWrite(getData());

    }

写入结果:学生表.xlsx

Java(18):easyExcel如何使用实体映射关系直接读写Excel数据

9.4写入数据后再执行读操作,示例

    public  static void readExcel(){

        List<SDKData> list = new ArrayList<>(); //"学生表" + ExcelTypeEnum.XLSX.getValue()

        EasyExcel.read("学生表.XLSX", SDKData.class, new AnalysisEventListener<SDKData>() {

            //每读取一行就调用该方法

            @Override

            public void invoke(SDKData data, AnalysisContext context) {

                list.add(data);

            }

            // 全部读取完成就调用该方法

            @Override

            public void doAfterAllAnalysed(AnalysisContext context) {

                System.out.println("读取Excel完成,行数:"+list.size());

            }

        }).sheet().doRead();

        for (SDKData data:list){

            System.out.println(data.getId()+" "+data.getName()+" "+data.getSalary()+" "+data.getBirthday());

       }

    }

读的结果:

 Java(18):easyExcel如何使用实体映射关系直接读写Excel数据

上一篇:Spring Boot + EasyExcel 导入导出


下一篇:easyExcel Java 操作遇到的部分问题记录