java 通过EasyExcel读取文件流,获取excel文件数据

这里展示两种读取方式,读取文件和网络文件

1.读取文件

 /**
     * @Des: 获取excel文件类型信息
     * @Author: liut
     * @Date: 2021/1/19 14:53
     */
    public Map getLocalFileInfo(MultipartFile file,String sheetNum){
        Map<String, Object> map = new HashMap<>();
        InputStream inputStream = null;
        List<Object> list = new ArrayList<>();
        List<String> sheetsName = new ArrayList<>();
        List<Sheet> sheets = new ArrayList<>();
        ExcelReader reader = null;
        ExcelListener excelListener = new ExcelListener();
        try {
            inputStream = file.getInputStream();
            reader = EasyExcelFactory.getReader(inputStream, excelListener);
            sheets = reader.getSheets();
            list = EasyExcelFactory.read(file.getInputStream(), new Sheet(Integer.valueOf(sheetNum)));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for (Sheet sheet : sheets) {
            String sheetName = sheet.getSheetName();
            sheetsName.add(sheetName);
        }
        //只展示前10条数据
        if (list.size() > 10){
            list.subList(0,10);
        }
        //存入所有sheet名称和指定的sheet数据
        map.put("sheetNum", sheetsName);
        map.put("content", list);
        return map;
    }

2.读取网络文件

/**
     * @Des: 获取网络excel文件数据
     * @Author: liut
     * @Date: 2021/1/19 14:54
     */
    public Map getOnlineFileInfo(String url, String sheetNum){
        Map<String, Object> map = new HashMap<>();
        URL conn = null;
        List<Object> list = new ArrayList<>();
        InputStream inputStream = null;
        URLConnection connection = null;
        List<Sheet> sheets = new ArrayList<>();
        List<String> sheetsName = new ArrayList<>();
        ExcelReader reader = null;
        ExcelListener excelListener = new ExcelListener();
        try {
            //获取所有sheet数据
            conn = new URL(url);
            connection = conn.openConnection();
            connection.setConnectTimeout(1000);
            inputStream = connection.getInputStream();
            InputStream inputStream1 = inputStream;
            reader = EasyExcelFactory.getReader(inputStream, excelListener);
            sheets = reader.getSheets();
            //获取指定sheet表数据
            conn = new URL(url);
            connection = conn.openConnection();
            connection.setConnectTimeout(1000);
            list = EasyExcelFactory.read(connection.getInputStream(), new Sheet(Integer.valueOf(sheetNum)));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //获取所有sheet名称和指定的sheet数据
        for (Sheet sheet : sheets) {
            sheetsName.add(sheet.getSheetName());
        }
        //只展示前10条数据
        if (list.size() > 10){
            list.subList(0,10);
        }
        //存入所有sheet名称和指定的sheet数据
        map.put("sheetsName", sheetsName);
        map.put("content", list);
        return map;
    }

其中ExcelListener代码如下:

/* 解析监听器,
 * 每解析一行会回调invoke()方法。
 * 整个excel解析结束会执行doAfterAllAnalysed()方法
 */
public class ExcelListener extends AnalysisEventListener {

    //自定义用于暂时存储data。
    //可以通过实例获取该值
    private List<Object> datas = new ArrayList<Object>();
    public void invoke(Object object, AnalysisContext context) {
        System.out.println("当前行:"+context.getCurrentRowNum());
        System.out.println(object);
        datas.add(object);//数据存储到list,供批量处理
        doSomething(object);//业务处理
    }

    private void doSomething(Object object) {
        //缓存到redids
    }

    public void doAfterAllAnalysed(AnalysisContext context) {
        // datas.clear();//解析结束销毁不用的资源
    }

    public List<Object> getDatas() {
        return datas;
    }

    public void setDatas(List<Object> datas) {
        this.datas = datas;
    }

}

监听器可以监听excel中的每行数据,并可根据业务需求对其进行相应操作

结果如下:

java 通过EasyExcel读取文件流,获取excel文件数据_第1张图片

你可能感兴趣的:(java,java,excel,easyexcel)