easyexcel 读取大excel 十万级 java
easyexcel
快速、简洁、解决大文件内存溢出的java处理Excel工具
项目地址:https://gitcode.com/gh_mirrors/ea/easyexcel
·
转发请注明: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
快速、简洁、解决大文件内存溢出的java处理Excel工具
最近提交(Master分支:4 个月前 )
c42183df
Bugfix 1 年前
efa7dff6 * 重新加回 `commons-io`
1 年前
AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。
更多推荐


所有评论(0)