Java implements EasyExcel to read the first few rows and the first few columns

original address

Original address: Java implements EasyExcel to read the first few rows and the first few columns

background

In the current project, there is a requirement for the preview operation of dataset files. Since it is a dataset, Excel files with large data volumes are also inevitable. It is normal to have hundreds of columns and tens of thousands of rows of data, so it is necessary to make a A metropolitan area plan with limited ranks and columns.

Because only the first few rows and the first few columns are read, the resource occupation and reading time are very short, so I want to directly read and process synchronously, but when EsayExcel reads synchronously, in addition to rewriting some classes by myself, it is registered for synchronization by default. Read the listener, so the consideration of using synchronous reading has not been realized before.

Later, I sent an ISSUES to the author boss under Github, and JiaJu Zhuang gave a reminder and threw an ExcelAnalysisStopException exception, so I followed this idea to do it.

accomplish

listener

Because of the constraints of synchronous reading, asynchronous reading is still used. First, a listener class is defined to realize the row and column restrictions during asynchronous reading. Here, LimitExcelReadListener is defined and AnalysisEventListener is inherited;

Because what I am doing is a dataset preview, the rows and columns are not fixed, and only Map can be used to receive rows. The code is not complicated, so I will give the complete code directly:

public class LimitExcelReadListener extends AnalysisEventListener<Map<Integer, String>> {
    
    
    // 定义变量,分别表示限制列数和行数
    private Integer limitColSize;
    private Integer limitRowSize;
    // 定义变量,存储表头信息和表数据
    private List<Map<Integer, String>> headList = new ArrayList<>();
    private List<Map<Integer, String>> dataList = new ArrayList<>();
    // 构造函数
    LimitExcelReadListener() {
    
    

    }
    // 带参构造函数,直接赋值限制行列
    LimitExcelReadListener(Integer limitColSize, Integer limitRowSize) {
    
    
        this.limitColSize = limitColSize;
        this.limitRowSize = limitRowSize;
    }
    
    /**
     * 工具方法,根据传入的列限制来保留数据,也就是只取前几个Key,如果有需求,可以自己添加KeySet排序之后再取,我这里没这个需求
     * @param oldMap 未限制列前的Map
     * @return 限制列后的Map
     */
    private Map<Integer, String> getLimitColMap(Map<Integer, String> oldMap) {
    
    
        Integer size = oldMap.keySet().size();
        Map<Integer, String> newMap = new HashMap<>();
        for (int i = 0; i < (size >= this.limitColSize ? this.limitColSize : size); i++) {
    
    
            newMap.put(i, oldMap.get(i));
        }
        return newMap;
    }

    @Override
    public void invoke(Map<Integer, String> integerStringMap, AnalysisContext analysisContext) {
    
    
        // 获取限制列之后的Map,并存入dataList
        Map<Integer, String> newMap = this.getLimitColMap(integerStringMap);
        dataList.add(newMap);
        
        // 判断行数已达到限制行数,抛出ExcelAnalysisException
        if (dataList.size() >= this.limitRowSize) {
    
    
            throw new ExcelAnalysisException(this.limitRowSize + "行" + this.limitColSize + "列读取完成");
        }
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
    
    
        // 获取限制列之后的表头Map,并存入headList
        Map<Integer, String> newMap = this.getLimitColMap(headMap);
        headList.add(newMap);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    
    
    }


}

Read tools

Then there is the tool class for reading, and the code is also very simple, so paste it directly here:

public class EExcelPrevUtils {
    
    
    /**
     * @param path         要读取文件的路径
     * @param limitColSize 要限制的列数
     * @param limitRowSize 要限制的行数
     * @return 读取到的数据
     * @throws FileNotFoundException
     */
    public static Map<String, Object> readLimitExcel(String path, Integer limitColSize, Integer limitRowSize) throws Exception {
    
    
        //定义一个Map保存表头和数据
        Map<String, Object> result = new HashMap<>();
        //声明读取所需FileInputStream
        FileInputStream inputStream = new FileInputStream(new File(path));
        //初始化一个监听器
        LimitExcelReadListener dataListener = new LimitExcelReadListener(limitColSize, limitRowSize);
        //读取文件数据
        try {
    
    
            EasyExcel.read(inputStream, dataListener).sheet().doRead();
        } catch (ExcelAnalysisException a) {
    
    
            System.out.println("读取完成");
            result.put("headList", dataListener.getHeadList());
            result.put("dataList", dataListener.getDataList());
        } catch (Exception a) {
    
    
            System.out.println("读取失败");
            throw a;
        }
        return result;
    }
}

other instructions

The above codes are based on the premise of one Sheet. If there are multiple sheets, just re-create the reading tool class according to your own needs.

thank you

Alibaba EasyExcel

JiaJu Zhuang

original address

Original address: Java implements EasyExcel to read the first few rows and the first few columns

Guess you like

Origin blog.csdn.net/u012751272/article/details/126298071