easyExcel百万级读取,

内容占用极低,不会导致OOM。
模拟请求:post请求
在这里插入图片描述

pom依赖,必须是对应版本:

   		<!-- 引入Easy Excel用于excel文件的读写 -->
   		<dependency>
		    <groupId>com.alibaba</groupId>
		    <artifactId>easyexcel</artifactId>
		    <version>2.0.1</version>
		</dependency> 

springboot文件大小限制配置:
springboot2.x版本,每个版本配置的key不一样

# set file size: max Size is 100Mb
spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=100MB

controller层代码

	@ApiOperation("xxxx")
	@PostMapping(value = "/getData", 
	        produces = "application/json;charset=utf-8")
	public ResponseEntity<CommonResult> getData(@RequestParam(value = "file", required = false) MultipartFile file, @RequestParam(value = "Id",required = true) Long Id)
	{	
		return new ResponseEntity<>(service.importData(file, Id), HttpStatus.OK);
	}

service层代码

	
		public CommonResult importData(MultipartFile file, Long Id)
	{
		long startReadExcelTime = System.currentTimeMillis();
		CompletableFuture.runAsync(()->{				
			try {
			InputStream ins = file.getInputStream();
			EasyExcel.read(ins, new CustomExcelListener(Id, DaoMapper)).sheet().doRead();
			log.info("ReadExcel cost :" + (System.currentTimeMillis() - startReadExcelTime));
			} 
        	catch (IOException e) {				
        		log.error("fail to imporData, the detail is: " + e);
			}
		});			
        return ReturnResult.buildSuccessResult(null);
	}

CustomExcelListener类:

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;

public class CustomExcelListener extends AnalysisEventListener<Map<Integer, String>> 
{
    public CustomExcelListener() {

    }
    /**
                * 每读取1000条做一次处理,然后清理list ,方便内存回收
     */
    private static final int BATCH_COUNT = 1000;
 
	List<String> columns = new ArrayList<>();

    List<String> content = new ArrayList<>();
    
    private Long Id;
    
    //业务相关。。。
	private DaoMapper daoMapper;
	
    public CustomExcelListener(Long Id, DaoMapper daoMapper) 
    {
	    this.Id = Id;
	    this.daoMapper= daoMapper
    }
        
    /**
     *DO: 读数据,是从第二行开始读的
     *
     */
    @Override
    public void invoke(Map<Integer, String> data, AnalysisContext context) 
    {  
    	try {
    	    //时间转换,第一列为时间
        	Calendar calendar = Calendar.getInstance();
			calendar.setTime(new SimpleDateFormat("yyyyMMddHHmmss").parse(data.get(0).toString()));
	    	calendar.getTimeInMillis();
			timestamps.add(calendar.getTimeInMillis());
			
			//map内容为:下标-内容
			content.add(data.get(i).toString());
			
	        if (content.size() >= BATCH_COUNT) 
	        {
	        	save();
	        }		
		} catch (ParseException e) {	
		}
    }

    /**
     * DO:读表头(列名)
     * */
    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) 
    {
		for (int i = 0; i < headMap.size(); i++) 
		{
			columns.add(headMap.get(i));
		}
    }
    
    /**
                * 所有数据解析完成了来调用
     *
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) 
    {  		
		save();
    }

	private void save() {
	daoMapper.save(content);
	}

}
发布了20 篇原创文章 · 获赞 0 · 访问量 1098
App 阅读领勋章
微信扫码 下载APP
阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 1024 设计师: 上身试试

分享到微信朋友圈

×

扫一扫,手机浏览