All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.rrsunhome.excelsql.parser.ExcelFileParser Maven / Gradle / Ivy

There is a newer version: 2.1.2
Show newest version
package com.github.rrsunhome.excelsql.parser;

import com.alibaba.excel.EasyExcel;
import com.github.rrsunhome.excelsql.config.ExcelParserConfig;
import com.github.rrsunhome.excelsql.config.BaseParserConfig;
import com.github.rrsunhome.excelsql.parser.support.BaseRowResultSet;
import com.github.rrsunhome.excelsql.parser.support.MapRowResultSet;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author : wangqijia
 * create at:  2021/10/26  下午5:22
 */
public class ExcelFileParser extends AbstractFileParser {

    @Override
    public String[] getSupportedFileExtensions() {
        return new String[]{"xls", "xlsx"};
    }

    @Override
    public BaseParserConfig getDefaultParserConfig() {
        return new ExcelParserConfig();
    }

    @Override
    protected List load(InputStream is, BaseParserConfig parserConfig) {
        ExcelParserConfig excelParserConfig = (ExcelParserConfig) parserConfig;
        List> mapList = readExcel(is, excelParserConfig);
        return toRowResultSet(mapList);
    }

    private List toRowResultSet(List> mapList) {
        List rowResultSets = new ArrayList<>(mapList.size());
        int index = 0;
        for (Map map : mapList) {
            rowResultSets.add(new MapRowResultSet(index, map));
            index++;
        }
        return rowResultSets;
    }


    protected List> readExcel(InputStream is, ExcelParserConfig excelParserConfig) {
        return EasyExcel.read(is)
                .sheet(excelParserConfig.getSheetIndex(), excelParserConfig.getSheetName())
                .headRowNumber(excelParserConfig.getRowRange().getStart())
                .doReadSync();

    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy