Skip to content

Commit

Permalink
优化读写性能
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Oct 29, 2021
1 parent 059bb9b commit e33afa6
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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模式,在上层做了模型转换的封装,让使用者更加简单方便

## 64M内存1分钟内读取75M(46W行25列)的Excel
## 64M内存20秒内读取75M(46W行25列)的Excel(3.0.2+版本)
当然还有极速模式能更快,但是内存占用会在100M多一点
![img](img/readme/large.png)

Expand All @@ -41,7 +41,7 @@ Java解析、生成Excel比较有名的框架有Apache poi、jxl。但他们都
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
</dependency>
```

Expand Down
Binary file modified img/readme/large.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.1</version>
<version>3.0.2</version>
<packaging>jar</packaging>
<name>easyexcel</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public class DataFormatter {
*/
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
* ("#").
Expand Down Expand Up @@ -644,8 +646,7 @@ private String getFormattedDateString(Double data, Short dataFormat, String data
*/
private String getFormattedNumberString(BigDecimal data, Short dataFormat, String dataFormatString) {
Format numberFormat = getFormat(data.doubleValue(), dataFormat, dataFormatString);
String formatted = numberFormat.format(data);
return formatted.replaceFirst("E(\\d)", "E+$1"); // to match Excel's E-notation
return E_NOTATION_PATTERN.matcher(numberFormat.format(data)).replaceFirst("E+$1");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class LargeDataTest {
private static File template07;
private static File fileCsv;
private static File fileWrite07;
private static File fileWriteTemp07;
private static File fileWritePoi07;

private int i = 0;
Expand All @@ -40,14 +41,14 @@ public class LargeDataTest {
public static void init() {
fileFill07 = TestFileUtil.createNewFile("largefill07.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");
template07 = TestFileUtil.readFile("large" + File.separator + "fill.xlsx");
fileCsv = TestFileUtil.createNewFile("largefileCsv.csv");
}

@Test
public void t01Read() throws Exception{
Thread.sleep(10*1000L);
public void t01Read() throws Exception {
long start = System.currentTimeMillis();
EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class,
new LargeDataListener()).headRowNumber(2).sheet().doRead();
Expand Down Expand Up @@ -86,9 +87,16 @@ public void t03ReadAndWriteCsv() {

@Test
public void t04Write() throws Exception {
long start = System.currentTimeMillis();
ExcelWriter excelWriter = EasyExcel.write(fileWrite07, LargeData.class).build();
ExcelWriter excelWriter = EasyExcel.write(fileWriteTemp07, LargeData.class).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++) {
excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j);
Expand Down Expand Up @@ -116,7 +124,8 @@ public void t04Write() throws Exception {
}
long costPoi = 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() {
Expand Down
2 changes: 1 addition & 1 deletion update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 3.0.2
* 修复写入的性能问题
* 大幅提升读写性能

# 3.0.1
* 升级到正式版
Expand Down

0 comments on commit e33afa6

Please sign in to comment.