项目要求导入excel, 但是文件很大,一次性读进去会导致内存不足而报错,
下面是我解决的方法:
首先倒入需要的jar
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>com.monitorjbl</groupId>
<artifactId>xlsx-streamer</artifactId>
<version>1.2.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.poi/openxml4j -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>openxml4j</artifactId>
<version>1.0-beta</version>
</dependency>
//报这个错需要引下面这个jar包
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>
借助第三方工具:StreamingReader
package com.quanzhen.test;
import com.monitorjbl.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileInputStream;
/**
* Created by Administrator on 2019/5/21 0021.
*/
public class Utf8Util {
public static void testLoad() throws Exception{
FileInputStream in = new FileInputStream("C:/Users/Administrator/Desktop/Dm_Mobile.xlsx");
Workbook wk = StreamingReader.builder()
.rowCacheSize(100) //缓存到内存中的行数,默认是10
.bufferSize(4096) //读取资源时,缓存到内存的字节大小,默认是1024
.open(in); //打开资源,必须,可以是InputStream或者是File,注意:只能打开XLSX格式的文件
Sheet sheet = wk.getSheetAt(0);
//遍历所有的行
for (Row row : sheet) {
System.out.println("开始遍历第" + row.getRowNum() + "行数据:");
//遍历所有的列
for (Cell cell : row) {
System.out.print(cell.getStringCellValue() + " ");
}
System.out.println(" ");
}
}
public static void main(String[] args) throws Exception {
testLoad();
}
}
刚开始报版本不支持52.0,网上说是因为我用的jdk是1.7的,要更新成1.8的,换成1.8的之后,还是一大堆错误,主要是因为
jar包没有全部导入,还有一个原因就是版本不一致,要是4.0都是4.0要是3.8都是3.8,
全部导入之后,把jdk切换成1.7和1.8,发现两个都是可以的,并不是jdk版本问题