如何优雅的使用easyexcel

问题痛点:一般都过easy excel 实现导入功能,不同的业务需要写不同的listener,这显然不够优雅,那么就让我们讨论下如何优雅的使用easy excel吧,优雅永不过时!

1、首先引入easy excel pom配置

<!--easy excel start-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>easyexcel</artifactId>
            <version>2.2.6</version>
        </dependency>
<!--easy excel end-->
复制代码

2、定义自己的function函数(不定义的话,可以使用java8 的consumer函数,但由于我们导入一般为list,使用consumer使用起来不够优雅)

/**
 * @author :Nickels
 * @date :2020/7/27
 * @desc :
 */
@FunctionalInterface
public interface ExcelConsumer<E> {

    void excute(List<E> e);
}
复制代码

3、编写listener
①listener基本根据git官网照抄的,唯一不同的是我们是以我们自定义的function作为一个变量

/**
 * @author :Nickels
 * @date :2020/7/27
 * @desc :
 */
// 有个很重要的点 DemoDataListener 不能被spring管理,要每次读取excel都要new,然后里面用到spring可以构造方法传进去
public class ExcelListener<T> extends AnalysisEventListener<T> {

    /**
     * 每隔5条存储数据库,实际使用中可以3000条,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 1000;
    List<T> list = new ArrayList<T>();
    /**
     * 假设这个是一个DAO,当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。这里我们传入自己的function作为参数
     */
    ExcelConsumer consumer;

    public ExcelListener() {
    }

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     */
    public ExcelListener(ExcelConsumer consumer) {
        this.consumer = consumer;
    }


    @Override
    public void invoke(T t, AnalysisContext analysisContext) {
        list.add(t);
        if (list.size() > BATCH_COUNT){
            //超出界限值,保存数据库,避免oom
            //执行函数
            consumer.excute(list);

            // 存储完成清理 list
            list.clear();

        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 这里也要保存数据,确保最后遗留的数据也存储到数据库
        consumer.excute(list);
    }

}
复制代码

4、自定义导入/导出模板

/**
 * @author :Nickels
 * @date :2020/7/27
 */
@Getter
@Setter
public class SendAddressTemplate {

    @ExcelProperty(value = "公司名称",index = 0)
    private String companyName;

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

    @ExcelProperty(value = "电话",index = 2)
    private String phone;

    @ExcelProperty(value = "详细地址",index = 3)
    private String addressDetail;

    @ExcelProperty(value = "备注",index = 4)
    private String remak;

}
复制代码

5、导入使用

EasyExcel.read(file.getInputStream(), SendAddressTemplate.class,new ExcelListener(p->{
                    //保存逻辑(可封装一个普通的sevice)p-为我们执行函数时传入的list
                    //sendAddressService.importExcel(p);
                })).sheet().doRead();
复制代码