转发请注明: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