easyexcel 读取大excel 十万级 java

转发请注明:https://blog.csdn.net/somdip/article/details/85243005

<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>easyexcel</artifactId>
		<version>1.0.4</version>
</dependency>
package com.navitek.crawer.excelTest;

import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.read.context.AnalysisContext;
import com.alibaba.excel.read.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.util.StringUtils;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author somdip
 * @since 1.0 2018-09-28
 */
public class EasyExcelTest {

    public static void main(String[] args) {

        long act = System.currentTimeMillis();
        String filepath ="E:\\all_cach\\31.xlsx";


        List<String> sheetContent = read(filepath);
        //write();

        System.out.println("一共"+sheetContent.size()+"有效数据");
        long end = System.currentTimeMillis();
        System.out.println("耗时间=======:"+(end-act)+"毫秒");
        
        System.out.println(listUser.size());


    }

    private static  List<String> read(String filepath) {
        List<String> sheetContent = new ArrayList<>();

        //String filepath ="E:\\all_cach\\1.xlsx";
        try (InputStream inputStream = new FileInputStream(filepath)) {
            ExcelReader excelReader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null,
                    new AnalysisEventListener<List<String>>() {

                @Override
                public void invoke(List<String> object, AnalysisContext context) {
                    /**
                     * 自行添加syl 》》》
                     */
                    StringBuffer stringBuffer = new StringBuffer();
                    if(object != null && !StringUtils.isEmpty(object.get(0))){
                        for(String s: object){
                            if(null!=s){
                                stringBuffer.append(s+"=");
                            }
                        }
                        System.out.println("行=="+context.getCurrentRowNum()+":  "+stringBuffer.toString());

                        sheetContent.add(stringBuffer.toString());
                        /**
                         *  《《《自行添加结束
                         */
                        System.out.println("当前sheet:" + context.getCurrentSheet().getSheetNo() + ",当前行:" +
                                context.getCurrentRowNum());
                    }

                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {

                }
            });
            excelReader.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    return sheetContent;

    }


}

亲测十万行16列左右大约耗费三秒左右,3列耗费1秒左右。

解决OOM(内存溢出)。

bug :对单元格中的“<”  ,“ >”敏感,有的话 数据发生错乱,目前无解,只能把列删去或者避免。

参考原文地址:https://blog.csdn.net/wang704987562/article/details/82887611

推荐HSSFWorkbook方式  https://blog.csdn.net/somdip/article/details/89814268

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

somdip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值