Browse Source

优化读写性能

pull/2159/head
Jiaju Zhuang 3 years ago
parent
commit
e33afa6c0f
  1. 4
      README.md
  2. BIN
      img/readme/large.png
  3. 2
      pom.xml
  4. 5
      src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java
  5. 19
      src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  6. 2
      update.md

4
README.md

@ -21,7 +21,7 @@ EasyExcel
# JAVA解析Excel工具EasyExcel # JAVA解析Excel工具EasyExcel
Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便 Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都存在一个严重的问题就是非常的耗内存,poi有一套SAX模式的API可以一定程度的解决一些内存溢出的问题,但POI还是有一些缺陷,比如07版Excel解压缩以及解压后存储都是在内存中完成的,内存消耗依然很大。easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析依然需要100M左右内存,改用easyexcel可以降低到几M,并且再大的excel也不会出现内存溢出;03版依赖POI的sax模式,在上层做了模型转换的封装,让使用者更加简单方便
## 64M内存1分钟内读取75M(46W行25列)的Excel ## 64M内存20秒内读取75M(46W行25列)的Excel(3.0.2+版本)
当然还有极速模式能更快,但是内存占用会在100M多一点 当然还有极速模式能更快,但是内存占用会在100M多一点
![img](img/readme/large.png) ![img](img/readme/large.png)
@ -41,7 +41,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>3.0.1</version> <version>3.0.2</version>
</dependency> </dependency>
``` ```

BIN
img/readme/large.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.3 KiB

After

Width:  |  Height:  |  Size: 57 KiB

2
pom.xml

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>3.0.1</version> <version>3.0.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>easyexcel</name> <name>easyexcel</name>

5
src/main/java/com/alibaba/excel/metadata/format/DataFormatter.java

@ -111,6 +111,8 @@ public class DataFormatter {
*/ */
private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})"); private static final Pattern alternateGrouping = Pattern.compile("([#0]([^.#0])[#0]{3})");
private static final Pattern E_NOTATION_PATTERN = Pattern.compile("E(\\d)");
/** /**
* Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs * Cells formatted with a date or time format and which contain invalid date or time values show 255 pound signs
* ("#"). * ("#").
@ -644,8 +646,7 @@ public class DataFormatter {
*/ */
private String getFormattedNumberString(BigDecimal data, Short dataFormat, String dataFormatString) { private String getFormattedNumberString(BigDecimal data, Short dataFormat, String dataFormatString) {
Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString); Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString);
String formatted = numberFormat.format(data); return E_NOTATION_PATTERN.matcher(numberFormat.format(data)).replaceFirst("E+$1");
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
} }
/** /**

19
src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -32,6 +32,7 @@ public class LargeDataTest {
private static File template07; private static File template07;
private static File fileCsv; private static File fileCsv;
private static File fileWrite07; private static File fileWrite07;
private static File fileWriteTemp07;
private static File fileWritePoi07; private static File fileWritePoi07;
private int i = 0; private int i = 0;
@ -40,14 +41,14 @@ public class LargeDataTest {
public static void init() { public static void init() {
fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx"); fileFill07 = TestFileUtil.createNewFile("largefill07.xlsx");
fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx"); fileWrite07 = TestFileUtil.createNewFile("large" + File.separator + "fileWrite07.xlsx");
fileWriteTemp07 = TestFileUtil.createNewFile("large" + File.separator + "fileWriteTemp07.xlsx");
fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx"); fileWritePoi07 = TestFileUtil.createNewFile("large" + File.separator + "fileWritePoi07.xlsx");
template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx"); template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx");
fileCsv = TestFileUtil.createNewFile("largefileCsv.csv"); fileCsv = TestFileUtil.createNewFile("largefileCsv.csv");
} }
@Test @Test
public void t01Read() throws Exception{ public void t01Read() throws Exception {
Thread.sleep(10*1000L);
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead(); new LargeDataListener()).headRowNumber(2).sheet().doRead();
@ -86,9 +87,16 @@ public class LargeDataTest {
@Test @Test
public void t04Write() throws Exception { public void t04Write() throws Exception {
long start = System.currentTimeMillis(); ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07, LargeData.class).build();
ExcelWriter excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 2; j++) {
excelWriter.write(data(), writeSheet);
}
excelWriter.finish();
long start = System.currentTimeMillis();
excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build();
writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 100; j++) { for (int j = 0; j < 100; j++) {
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j); LOGGER.info("{} write success.", j);
@ -116,7 +124,8 @@ public class LargeDataTest {
} }
long costPoi = System.currentTimeMillis() - start; long costPoi = System.currentTimeMillis() - start;
LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start); LOGGER.info("poi write cost:{}", System.currentTimeMillis() - start);
Assert.assertTrue(costPoi * 3 > cost); LOGGER.info("{} vs {}", cost, costPoi);
Assert.assertTrue(costPoi * 2 > cost);
} }
private List<LargeData> data() { private List<LargeData> data() {

2
update.md

@ -1,5 +1,5 @@
# 3.0.2 # 3.0.2
* 修复写入的性能问题 * 大幅提升读写性能
# 3.0.1 # 3.0.1
* 升级到正式版 * 升级到正式版

Loading…
Cancel
Save