easyExcel导入类

一、基础类描述

  1. ExcelImport(导入工具类)
  2. ExcelRow(导入实体类)
  3. Consumer(函数参数)

二、代码

1、ExcelImport


import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.itxl.common.utils.Consumer;

import java.util.ArrayList;
import java.util.List;

/**
 * @authar liuWei
 * @ @date 2024/2/1
 * 导入
 */
public abstract class ExcelImport extends AnalysisEventListener<ExcelRow> implements Consumer<Void> {

    private int startRow = 1;

    /**
     * 一直执行最大LIST数量
     */
    private final static int ONE_EXECUTE_LIST_SIZE = 1000;

    private final List<ExcelRow> data = new ArrayList<>();

    public ExcelImport(int startRow){
        if (startRow < 1){
            //todo 报错,导入开始行必须不能小于1
        }
        this.startRow = startRow;
    }

    public ExcelImport(){

    }
    @Override
    public void invoke(ExcelRow excelRow, AnalysisContext analysisContext) {
        if (analysisContext.readRowHolder().getRowIndex() < startRow){
            return;
        }
        data.add(excelRow);
        if (data.size() >= ONE_EXECUTE_LIST_SIZE){
            accept();
            data.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        accept();
        data.clear();
    }

    @Override
    public Void accept() {
        return andThen();
    }
}

2、ExcelRow

import lombok.Data;

/**
 * @authar liuWei
 * @ @date 2024/2/1
 * 导入映射实体
 */
@Data
public class ExcelRow {
    private String col1;
    private String col2;
    private String col3;
    private String col4;
    private String col5;
    private String col6;
    private String col7;
    private String col8;
    private String col9;
    private String col10;
    private String col11;
    private String col12;
    private String col13;
    private String col14;
    private String col15;
    private String col16;
    private String col17;
    private String col18;
    private String col19;
    private String col20;
    private String col21;
    private String col22;
    private String col23;
    private String col24;
    private String col25;
    private String col26;
    private String col27;
    private String col28;
    private String col29;
    private String col30;
    private String col31;
    private String col32;
    private String col33;
    private String col34;
    private String col35;
    private String col36;
    private String col37;
    private String col38;
    private String col39;
    private String col40;
    private String col41;
    private String col42;
    private String col43;
    private String col44;
    private String col45;
    private String col46;
    private String col47;
    private String col48;
    private String col49;
    private String col50;
    private String col51;
    private String col52;
    private String col53;
    private String col54;
    private String col55;
    private String col56;
    private String col57;
    private String col58;
    private String col59;
    private String col60;
    private String col61;
    private String col62;
    private String col63;
    private String col64;
    private String col65;
    private String col66;
    private String col67;
    private String col68;
    private String col69;
    private String col70;
    private String col71;
    private String col72;
    private String col73;
    private String col74;
    private String col75;
    private String col76;
    private String col77;
    private String col78;
    private String col79;
    private String col80;
    private String col81;
    private String col82;
    private String col83;
    private String col84;
    private String col85;
    private String col86;
    private String col87;
    private String col88;
    private String col89;
    private String col90;
    private String col91;
    private String col92;
    private String col93;
    private String col94;
    private String col95;
    private String col96;
    private String col97;
    private String col98;
    private String col99;
    private String col100;
}

3、Consumer


/**
 * @authar  liuWei
 * 通用函数参数
 * @param <T>
 */
public interface Consumer<T>{
    /**
     * 方法实现
     * @return T
     */
    T andThen();

    /**
     * 方法执行
     * @return T
     */
    T accept();
}

posted @ 2024-04-05 19:47  刘子越  阅读(81)  评论(0编辑  收藏  举报