Browse Source

Merge pull request #2965 from alibaba/bugfix

Bugfix
pull/2514/merge v3.2.1
Jiaju Zhuang 2 years ago committed by GitHub
parent
commit
5fa867b1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .github/ISSUE_TEMPLATE/bug.md
  2. 1
      .github/ISSUE_TEMPLATE/question.md
  3. 2
      .github/workflows/sync2gitee.yml
  4. 4
      README.md
  5. 2
      easyexcel-core/pom.xml
  6. 16
      easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java
  7. 15
      easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java
  8. 2
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java
  9. 54
      easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java
  10. 62
      easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java
  11. 10
      easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java
  12. 36
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateDateConverter.java
  13. 57
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateNumberConverter.java
  14. 52
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateStringConverter.java
  15. 4
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java
  16. 2
      easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeNumberConverter.java
  17. 2
      easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java
  18. 16
      easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java
  19. 74
      easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java
  20. 2
      easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java
  21. 7
      easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java
  22. 14
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  23. 4
      easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java
  24. 1
      easyexcel-support/pom.xml
  25. 5
      easyexcel-test/pom.xml
  26. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java
  27. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java
  28. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java
  29. 43
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java
  30. 13
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  31. 8
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java
  32. 3
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java
  33. 3
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java
  34. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java
  35. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataListener.java
  36. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java
  37. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java
  38. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java
  39. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java
  40. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java
  41. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java
  42. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java
  43. 6
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  44. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java
  45. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java
  46. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java
  47. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java
  48. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java
  49. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java
  50. 21
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java
  51. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java
  52. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java
  53. 1
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java
  54. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java
  55. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java
  56. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java
  57. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java
  58. 4
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java
  59. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java
  60. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java
  61. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java
  62. 5
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java
  63. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java
  64. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java
  65. 7
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  66. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java
  67. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java
  68. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java
  69. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java
  70. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java
  71. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java
  72. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java
  73. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java
  74. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java
  75. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java
  76. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java
  77. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java
  78. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java
  79. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java
  80. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java
  81. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java
  82. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/TestListener.java
  83. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgListener.java
  84. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java
  85. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java
  86. 6
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java
  87. 126
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java
  88. 36
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java
  89. 1
      easyexcel/pom.xml
  90. BIN
      img/readme/alipay.png
  91. BIN
      img/readme/large.png
  92. 8
      pom.xml
  93. 10
      update.md

2
.github/ISSUE_TEMPLATE/bug.md

@ -16,3 +16,5 @@ assignees: zhuangjiaju
这里写代码 这里写代码
``` ```
# 提示的异常或者没有达到的效果 # 提示的异常或者没有达到的效果
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。

1
.github/ISSUE_TEMPLATE/question.md

@ -15,4 +15,5 @@ assignees: ''
``` ```
# 异常提示 # 异常提示
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。
# 问题描述 # 问题描述

2
.github/workflows/sync2gitee.yml

@ -1,6 +1,6 @@
# 通过 Github action, 在仓库的每一次 commit 后自动同步到 Gitee 上 # 通过 Github action, 在仓库的每一次 commit 后自动同步到 Gitee 上
name: Mirror the Github organization repos to Gitee name: Mirror the Github organization repos to Gitee
on: [push, pull_request] on: [push]
jobs: jobs:
repo-sync: repo-sync:

4
README.md

@ -15,7 +15,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
* github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel) * github地址:[https://github.com/alibaba/easyexcel](https://github.com/alibaba/easyexcel)
* gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel) * gitee地址:[https://gitee.com/easyexcel/easyexcel](https://gitee.com/easyexcel/easyexcel)
# 64M内存20秒读取75M(46W行25列)的Excel(3.0.2+版本) # 16M内存23秒读取75M(46W行25列)的Excel(3.2.1+版本)
当然还有[极速模式](https://easyexcel.opensource.alibaba.com/qa/read#%E5%BC%80%E5%90%AF%E6%80%A5%E9%80%9F%E6%A8%A1%E5%BC%8F) 当然还有[极速模式](https://easyexcel.opensource.alibaba.com/qa/read#%E5%BC%80%E5%90%AF%E6%80%A5%E9%80%9F%E6%A8%A1%E5%BC%8F)
能更快,但是内存占用会在100M多一点 能更快,但是内存占用会在100M多一点
@ -28,7 +28,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<version>3.2.0</version> <version>3.2.1</version>
</dependency> </dependency>
``` ```

2
easyexcel-core/pom.xml

@ -3,7 +3,6 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<parent> <parent>
<groupId>com.alibaba</groupId> <groupId>com.alibaba</groupId>
<artifactId>easyexcel-parent</artifactId> <artifactId>easyexcel-parent</artifactId>
@ -11,6 +10,7 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<url>https://github.com/alibaba/easyexcel</url>
<packaging>jar</packaging> <packaging>jar</packaging>
<artifactId>easyexcel-core</artifactId> <artifactId>easyexcel-core</artifactId>
<name>easyexcel-core</name> <name>easyexcel-core</name>

16
easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java

@ -11,11 +11,14 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import lombok.extern.slf4j.Slf4j;
/** /**
* Excel readers are all read in event mode. * Excel readers are all read in event mode.
* *
* @author jipengfei * @author jipengfei
*/ */
@Slf4j
public class ExcelReader implements Closeable { public class ExcelReader implements Closeable {
/** /**
@ -104,4 +107,17 @@ public class ExcelReader implements Closeable {
public void close() { public void close() {
finish(); finish();
} }
/**
* Prevents calls to {@link #finish} from freeing the cache
*
*/
@Override
protected void finalize() {
try {
finish();
} catch (Throwable e) {
log.warn("Destroy object failed", e);
}
}
} }

15
easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java

@ -12,6 +12,8 @@ import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillConfig;
import lombok.extern.slf4j.Slf4j;
/** /**
* Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two * Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two
* functions. * functions.
@ -23,6 +25,7 @@ import com.alibaba.excel.write.metadata.fill.FillConfig;
* *
* @author jipengfei * @author jipengfei
*/ */
@Slf4j
public class ExcelWriter implements Closeable { public class ExcelWriter implements Closeable {
private final ExcelBuilder excelBuilder; private final ExcelBuilder excelBuilder;
@ -154,4 +157,16 @@ public class ExcelWriter implements Closeable {
public void close() { public void close() {
finish(); finish();
} }
/**
* Prevents calls to {@link #finish} from freeing the cache
*/
@Override
protected void finalize() {
try {
finish();
} catch (Throwable e) {
log.warn("Destroy object failed", e);
}
}
} }

2
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java

@ -16,6 +16,6 @@ public class BoundSheetRecordHandler extends AbstractXlsRecordHandler implements
@Override @Override
public void processRecord(XlsReadContext xlsReadContext, Record record) { public void processRecord(XlsReadContext xlsReadContext, Record record) {
BoundSheetRecord bsr = (BoundSheetRecord)record; BoundSheetRecord bsr = (BoundSheetRecord)record;
xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add((BoundSheetRecord)record); xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add(bsr);
} }
} }

54
easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java vendored

@ -2,6 +2,7 @@ package com.alibaba.excel.cache;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
@ -15,6 +16,7 @@ import org.ehcache.config.CacheConfiguration;
import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.config.units.MemoryUnit; import org.ehcache.config.units.MemoryUnit;
/** /**
@ -35,6 +37,8 @@ public class Ehcache implements ReadCache {
private static final CacheManager FILE_CACHE_MANAGER; private static final CacheManager FILE_CACHE_MANAGER;
private static final CacheConfiguration<Integer, ArrayList> FILE_CACHE_CONFIGURATION; private static final CacheConfiguration<Integer, ArrayList> FILE_CACHE_CONFIGURATION;
private static final CacheManager ACTIVE_CACHE_MANAGER; private static final CacheManager ACTIVE_CACHE_MANAGER;
private static final File CACHE_PATH_FILE;
private final CacheConfiguration<Integer, ArrayList> activeCacheConfiguration; private final CacheConfiguration<Integer, ArrayList> activeCacheConfiguration;
/** /**
* Bulk storage data * Bulk storage data
@ -50,29 +54,61 @@ public class Ehcache implements ReadCache {
*/ */
private int cacheMiss = 0; private int cacheMiss = 0;
public Ehcache(int maxCacheActivateSize) { @Deprecated
activeCacheConfiguration = CacheConfigurationBuilder public Ehcache(Integer maxCacheActivateSize) {
this(maxCacheActivateSize, null);
}
public Ehcache(Integer maxCacheActivateSize, Integer maxCacheActivateBatchCount) {
// In order to be compatible with the code
// If the user set up `maxCacheActivateSize`, then continue using it
if (maxCacheActivateSize != null) {
this.activeCacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class, .newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder().heap(maxCacheActivateSize, MemoryUnit.MB)) ResourcePoolsBuilder.newResourcePoolsBuilder()
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(maxCacheActivateSize, MemoryUnit.MB) .heap(maxCacheActivateSize, MemoryUnit.MB))
.build(); .build();
} else {
this.activeCacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class,
ResourcePoolsBuilder.newResourcePoolsBuilder()
.heap(maxCacheActivateBatchCount, EntryUnit.ENTRIES))
.build();
}
} }
static { static {
File cacheFile = FileUtils.createCacheTmpFile(); CACHE_PATH_FILE = FileUtils.createCacheTmpFile();
FILE_CACHE_MANAGER = FILE_CACHE_MANAGER =
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true); CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(
true);
ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true);
FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder
.newCacheConfigurationBuilder(Integer.class, ArrayList.class, .newCacheConfigurationBuilder(Integer.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder()
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) .disk(20, MemoryUnit.GB)).build();
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build();
} }
@Override @Override
public void init(AnalysisContext analysisContext) { public void init(AnalysisContext analysisContext) {
cacheAlias = UUID.randomUUID().toString(); cacheAlias = UUID.randomUUID().toString();
try {
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION); fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
} catch (IllegalStateException e) {
//fix Issue #2693,Temporary files may be deleted if there is no operation for a long time, so they need
// to be recreated.
if (CACHE_PATH_FILE.exists()) {
throw e;
}
synchronized (Ehcache.class) {
if (!CACHE_PATH_FILE.exists()) {
if (log.isDebugEnabled()) {
log.debug("cache file dir is not exist retry create");
}
FileUtils.createDirectory(CACHE_PATH_FILE);
}
}
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION);
}
activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration); activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration);
} }

62
easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java vendored

@ -2,6 +2,10 @@ package com.alibaba.excel.cache.selector;
import java.io.IOException; import java.io.IOException;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackagePart;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -15,6 +19,9 @@ import com.alibaba.excel.cache.ReadCache;
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
**/ **/
@Getter
@Setter
@EqualsAndHashCode
public class SimpleReadCacheSelector implements ReadCacheSelector { public class SimpleReadCacheSelector implements ReadCacheSelector {
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class); private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class);
/** /**
@ -24,38 +31,47 @@ public class SimpleReadCacheSelector implements ReadCacheSelector {
/** /**
* If it's less than 5M, use map cache, or use ehcache.unit MB. * If it's less than 5M, use map cache, or use ehcache.unit MB.
*/ */
private static final int DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5; private static final long DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5;
/** /**
* Maximum size of cache activation.unit MB. * Maximum batch of `SharedStrings` stored in memory.
* The batch size is 100.{@link Ehcache#BATCH_COUNT}
*/ */
private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE = 20; private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT = 20;
/** /**
* Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB. * Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB.
*/ */
private final long maxUseMapCacheSize; private Long maxUseMapCacheSize;
/** /**
* Maximum size of cache activation.unit MB. * Maximum size of cache activation.unit MB.
*
* @deprecated Please use maxCacheActivateBatchCount to control the size of the occupied memory
*/
@Deprecated
private Integer maxCacheActivateSize;
/**
* Maximum batch of `SharedStrings` stored in memory.
* The batch size is 100.{@link Ehcache#BATCH_COUNT}
*/ */
private final int maxCacheActivateSize; private Integer maxCacheActivateBatchCount;
public SimpleReadCacheSelector() { public SimpleReadCacheSelector() {
this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE);
} }
public SimpleReadCacheSelector(long maxUseMapCacheSize, int maxCacheActivateSize) { /**
if (maxUseMapCacheSize <= 0) { * Parameter maxCacheActivateSize has already been abandoned
this.maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE; *
} else { * @param maxUseMapCacheSize
* @param maxCacheActivateSize
*/
@Deprecated
public SimpleReadCacheSelector(Long maxUseMapCacheSize, Integer maxCacheActivateSize) {
this.maxUseMapCacheSize = maxUseMapCacheSize; this.maxUseMapCacheSize = maxUseMapCacheSize;
}
if (maxCacheActivateSize <= 0) {
this.maxCacheActivateSize = DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE;
} else {
this.maxCacheActivateSize = maxCacheActivateSize; this.maxCacheActivateSize = maxCacheActivateSize;
} }
}
@Override @Override
public ReadCache readCache(PackagePart sharedStringsTablePackagePart) { public ReadCache readCache(PackagePart sharedStringsTablePackagePart) {
@ -68,6 +84,9 @@ public class SimpleReadCacheSelector implements ReadCacheSelector {
return new MapCache(); return new MapCache();
} }
} }
if (maxUseMapCacheSize == null) {
maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE;
}
if (size < maxUseMapCacheSize * B2M) { if (size < maxUseMapCacheSize * B2M) {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Use map cache.size:{}", size); LOGGER.debug("Use map cache.size:{}", size);
@ -77,6 +96,17 @@ public class SimpleReadCacheSelector implements ReadCacheSelector {
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Use ehcache.size:{}", size); LOGGER.debug("Use ehcache.size:{}", size);
} }
return new Ehcache(maxCacheActivateSize);
// In order to be compatible with the code
// If the user set up `maxCacheActivateSize`, then continue using it
if (maxCacheActivateSize != null) {
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount);
} else {
if (maxCacheActivateBatchCount == null) {
maxCacheActivateBatchCount = DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT;
}
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount);
}
} }
} }

10
easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java

@ -31,7 +31,10 @@ import com.alibaba.excel.converters.inputstream.InputStreamImageConverter;
import com.alibaba.excel.converters.integer.IntegerBooleanConverter; import com.alibaba.excel.converters.integer.IntegerBooleanConverter;
import com.alibaba.excel.converters.integer.IntegerNumberConverter; import com.alibaba.excel.converters.integer.IntegerNumberConverter;
import com.alibaba.excel.converters.integer.IntegerStringConverter; import com.alibaba.excel.converters.integer.IntegerStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateNumberConverter; import com.alibaba.excel.converters.localdate.LocalDateDateConverter;
import com.alibaba.excel.converters.localdate.LocalDateNumberConverter;
import com.alibaba.excel.converters.localdate.LocalDateStringConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeNumberConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter; import com.alibaba.excel.converters.localdatetime.LocalDateTimeDateConverter;
import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter; import com.alibaba.excel.converters.localdatetime.LocalDateTimeStringConverter;
import com.alibaba.excel.converters.longconverter.LongBooleanConverter; import com.alibaba.excel.converters.longconverter.LongBooleanConverter;
@ -83,6 +86,9 @@ public class DefaultConverterLoader {
putAllConverter(new DateStringConverter()); putAllConverter(new DateStringConverter());
putAllConverter(new LocalDateNumberConverter()); putAllConverter(new LocalDateNumberConverter());
putAllConverter(new LocalDateStringConverter());
putAllConverter(new LocalDateTimeNumberConverter());
putAllConverter(new LocalDateTimeStringConverter()); putAllConverter(new LocalDateTimeStringConverter());
putAllConverter(new DoubleBooleanConverter()); putAllConverter(new DoubleBooleanConverter());
@ -121,6 +127,7 @@ public class DefaultConverterLoader {
putWriteConverter(new ByteNumberConverter()); putWriteConverter(new ByteNumberConverter());
putWriteConverter(new DateDateConverter()); putWriteConverter(new DateDateConverter());
putWriteConverter(new LocalDateTimeDateConverter()); putWriteConverter(new LocalDateTimeDateConverter());
putWriteConverter(new LocalDateDateConverter());
putWriteConverter(new DoubleNumberConverter()); putWriteConverter(new DoubleNumberConverter());
putWriteConverter(new FloatNumberConverter()); putWriteConverter(new FloatNumberConverter());
putWriteConverter(new IntegerNumberConverter()); putWriteConverter(new IntegerNumberConverter());
@ -139,6 +146,7 @@ public class DefaultConverterLoader {
putWriteStringConverter(new BooleanStringConverter()); putWriteStringConverter(new BooleanStringConverter());
putWriteStringConverter(new ByteStringConverter()); putWriteStringConverter(new ByteStringConverter());
putWriteStringConverter(new DateStringConverter()); putWriteStringConverter(new DateStringConverter());
putWriteStringConverter(new LocalDateStringConverter());
putWriteStringConverter(new LocalDateTimeStringConverter()); putWriteStringConverter(new LocalDateTimeStringConverter());
putWriteStringConverter(new DoubleStringConverter()); putWriteStringConverter(new DoubleStringConverter());
putWriteStringConverter(new FloatStringConverter()); putWriteStringConverter(new FloatStringConverter());

36
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateDateConverter.java

@ -0,0 +1,36 @@
package com.alibaba.excel.converters.localdate;
import java.time.LocalDate;
import java.time.LocalDateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.WorkBookUtil;
/**
* LocalDate and date converter
*
* @author Jiaju Zhuang
*/
public class LocalDateDateConverter implements Converter<LocalDate> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws Exception {
LocalDateTime localDateTime = value == null ? null : value.atTime(0, 0);
WriteCellData<?> cellData = new WriteCellData<>(localDateTime);
String format = null;
if (contentProperty != null && contentProperty.getDateTimeFormatProperty() != null) {
format = contentProperty.getDateTimeFormatProperty().getFormat();
}
WorkBookUtil.fillDataFormat(cellData, format, DateUtils.defaultLocalDateFormat);
return cellData;
}
}

57
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateNumberConverter.java

@ -0,0 +1,57 @@
package com.alibaba.excel.converters.localdate;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import org.apache.poi.ss.usermodel.DateUtil;
/**
* LocalDate and number converter
*
* @author Jiaju Zhuang
*/
public class LocalDateNumberConverter implements Converter<LocalDate> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.NUMBER;
}
@Override
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
globalConfiguration.getUse1904windowing());
} else {
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
}
}
@Override
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(
BigDecimal.valueOf(DateUtil.getExcelDate(value, globalConfiguration.getUse1904windowing())));
} else {
return new WriteCellData<>(BigDecimal.valueOf(
DateUtil.getExcelDate(value, contentProperty.getDateTimeFormatProperty().getUse1904windowing())));
}
}
}

52
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateStringConverter.java

@ -0,0 +1,52 @@
package com.alibaba.excel.converters.localdate;
import java.text.ParseException;
import java.time.LocalDate;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
/**
* LocalDate and string converter
*
* @author Jiaju Zhuang
*/
public class LocalDateStringConverter implements Converter<LocalDate> {
@Override
public Class<?> supportJavaTypeKey() {
return LocalDate.class;
}
@Override
public CellDataTypeEnum supportExcelTypeKey() {
return CellDataTypeEnum.STRING;
}
@Override
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) throws ParseException {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return DateUtils.parseLocalDate(cellData.getStringValue(), null, globalConfiguration.getLocale());
} else {
return DateUtils.parseLocalDate(cellData.getStringValue(),
contentProperty.getDateTimeFormatProperty().getFormat(), globalConfiguration.getLocale());
}
}
@Override
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
GlobalConfiguration globalConfiguration) {
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
return new WriteCellData<>(DateUtils.format(value, null, globalConfiguration.getLocale()));
} else {
return new WriteCellData<>(
DateUtils.format(value, contentProperty.getDateTimeFormatProperty().getFormat(),
globalConfiguration.getLocale()));
}
}
}

4
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java

@ -10,13 +10,13 @@ import com.alibaba.excel.util.DateUtils;
import com.alibaba.excel.util.WorkBookUtil; import com.alibaba.excel.util.WorkBookUtil;
/** /**
* Date and date converter * LocalDateTime and date converter
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class LocalDateTimeDateConverter implements Converter<LocalDateTime> { public class LocalDateTimeDateConverter implements Converter<LocalDateTime> {
@Override @Override
public Class<LocalDateTime> supportJavaTypeKey() { public Class<?> supportJavaTypeKey() {
return LocalDateTime.class; return LocalDateTime.class;
} }

2
easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java → easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeNumberConverter.java

@ -18,7 +18,7 @@ import org.apache.poi.ss.usermodel.DateUtil;
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class LocalDateNumberConverter implements Converter<LocalDateTime> { public class LocalDateTimeNumberConverter implements Converter<LocalDateTime> {
@Override @Override
public Class<?> supportJavaTypeKey() { public Class<?> supportJavaTypeKey() {

2
easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java

@ -17,7 +17,7 @@ import lombok.Setter;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
/** /**
* wirte cell data * write cell data
* *
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */

16
easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java

@ -15,7 +15,7 @@ import org.apache.commons.collections4.CollectionUtils;
*/ */
public class PageReadListener<T> implements ReadListener<T> { public class PageReadListener<T> implements ReadListener<T> {
/** /**
* Single handle the amount of data * Defuault single handle the amount of data
*/ */
public static int BATCH_COUNT = 100; public static int BATCH_COUNT = 100;
/** /**
@ -27,16 +27,26 @@ public class PageReadListener<T> implements ReadListener<T> {
*/ */
private final Consumer<List<T>> consumer; private final Consumer<List<T>> consumer;
/**
* Single handle the amount of data
*/
private final int batchCount;
public PageReadListener(Consumer<List<T>> consumer) { public PageReadListener(Consumer<List<T>> consumer) {
this(consumer, BATCH_COUNT);
}
public PageReadListener(Consumer<List<T>> consumer, int batchCount) {
this.consumer = consumer; this.consumer = consumer;
this.batchCount = batchCount;
} }
@Override @Override
public void invoke(T data, AnalysisContext context) { public void invoke(T data, AnalysisContext context) {
cachedDataList.add(data); cachedDataList.add(data);
if (cachedDataList.size() >= BATCH_COUNT) { if (cachedDataList.size() >= batchCount) {
consumer.accept(cachedDataList); consumer.accept(cachedDataList);
cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount);
} }
} }

74
easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java

@ -4,7 +4,9 @@ import java.math.BigDecimal;
import java.text.DateFormat; import java.text.DateFormat;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.Date; import java.util.Date;
@ -59,6 +61,8 @@ public class DateUtils {
public static String defaultDateFormat = DATE_FORMAT_19; public static String defaultDateFormat = DATE_FORMAT_19;
public static String defaultLocalDateFormat = DATE_FORMAT_10;
private DateUtils() {} private DateUtils() {}
/** /**
@ -95,6 +99,25 @@ public class DateUtils {
} }
} }
/**
* convert string to date
*
* @param dateString
* @param dateFormat
* @param local
* @return
*/
public static LocalDate parseLocalDate(String dateString, String dateFormat, Locale local) {
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = switchDateFormat(dateString);
}
if (local == null) {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat));
} else {
return LocalDate.parse(dateString, DateTimeFormatter.ofPattern(dateFormat, local));
}
}
/** /**
* convert string to date * convert string to date
* *
@ -188,6 +211,38 @@ public class DateUtils {
} }
} }
/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDate date, String dateFormat) {
return format(date, dateFormat, null);
}
/**
* Format date
*
* @param date
* @param dateFormat
* @return
*/
public static String format(LocalDate date, String dateFormat, Locale local) {
if (date == null) {
return null;
}
if (StringUtils.isEmpty(dateFormat)) {
dateFormat = defaultLocalDateFormat;
}
if (local == null) {
return date.format(DateTimeFormatter.ofPattern(dateFormat));
} else {
return date.format(DateTimeFormatter.ofPattern(dateFormat, local));
}
}
/** /**
* Format date * Format date
* *
@ -271,6 +326,25 @@ public class DateUtils {
return DateUtil.getLocalDateTime(date, use1904windowing, true); return DateUtil.getLocalDateTime(date, use1904windowing, true);
} }
/**
* Given an Excel date with either 1900 or 1904 date windowing,
* converts it to a java.time.LocalDate.
*
* Excel Dates and Times are stored without any timezone
* information. If you know (through other means) that your file
* uses a different TimeZone to the system default, you can use
* this version of the getJavaDate() method to handle it.
*
* @param date The Excel date.
* @param use1904windowing true if date uses 1904 windowing,
* or false if using 1900 date windowing.
* @return Java representation of the date, or null if date is not a valid Excel date
*/
public static LocalDate getLocalDate(double date, boolean use1904windowing) {
LocalDateTime localDateTime = getLocalDateTime(date, use1904windowing);
return localDateTime == null ? null : localDateTime.toLocalDate();
}
/** /**
* Determine if it is a date format. * Determine if it is a date format.
* *

2
easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java

@ -174,7 +174,7 @@ public class FileUtils {
* *
* @param directory * @param directory
*/ */
private static File createDirectory(File directory) { public static File createDirectory(File directory) {
if (!directory.exists() && !directory.mkdirs()) { if (!directory.exists() && !directory.mkdirs()) {
throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath()); throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath());
} }

7
easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java

@ -20,8 +20,7 @@ public class SheetUtils {
/** /**
* Match the parameters to the actual sheet * Match the parameters to the actual sheet
* *
* @param readSheet * @param readSheet actual sheet
* actual sheet
* @param analysisContext * @param analysisContext
* @return * @return
*/ */
@ -48,10 +47,12 @@ public class SheetUtils {
boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim())
|| (parameterReadSheet.getAutoTrim() == null || (parameterReadSheet.getAutoTrim() == null
&& analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim());
String sheetName = readSheet.getSheetName();
if (autoTrim) { if (autoTrim) {
parameterSheetName = parameterSheetName.trim(); parameterSheetName = parameterSheetName.trim();
sheetName = sheetName.trim();
} }
match = parameterSheetName.equals(readSheet.getSheetName()); match = parameterSheetName.equals(sheetName);
} }
} }
if (match) { if (match) {

14
easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java

@ -66,7 +66,6 @@ public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder<Exce
return this; return this;
} }
public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) { public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) {
writeWorkbook.setExcelType(excelType); writeWorkbook.setExcelType(excelType);
return this; return this;
@ -95,21 +94,32 @@ public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder<Exce
return this; return this;
} }
/**
* Template file.
* This file is read into memory, excessive cases can lead to OOM.
*/
public ExcelWriterBuilder withTemplate(InputStream templateInputStream) { public ExcelWriterBuilder withTemplate(InputStream templateInputStream) {
writeWorkbook.setTemplateInputStream(templateInputStream); writeWorkbook.setTemplateInputStream(templateInputStream);
return this; return this;
} }
/**
* Template file.
* This file is read into memory, excessive cases can lead to OOM.
*/
public ExcelWriterBuilder withTemplate(File templateFile) { public ExcelWriterBuilder withTemplate(File templateFile) {
writeWorkbook.setTemplateFile(templateFile); writeWorkbook.setTemplateFile(templateFile);
return this; return this;
} }
/**
* Template file.
* This file is read into memory, excessive cases can lead to OOM.
*/
public ExcelWriterBuilder withTemplate(String pathName) { public ExcelWriterBuilder withTemplate(String pathName) {
return withTemplate(new File(pathName)); return withTemplate(new File(pathName));
} }
public ExcelWriter build() { public ExcelWriter build() {
return new ExcelWriter(writeWorkbook); return new ExcelWriter(writeWorkbook);
} }

4
easyexcel-core/src/main/java/com/alibaba/excel/write/metadata/WriteWorkbook.java

@ -49,7 +49,8 @@ public class WriteWorkbook extends WriteBasicParameter {
private InputStream templateInputStream; private InputStream templateInputStream;
/** /**
* Template file * Template file.
* This file is read into memory, excessive cases can lead to OOM.
* <p> * <p>
* If 'inputStream' and 'file' all not empty, file first * If 'inputStream' and 'file' all not empty, file first
*/ */
@ -66,7 +67,6 @@ public class WriteWorkbook extends WriteBasicParameter {
* Whether the encryption * Whether the encryption
* <p> * <p>
* WARRING:Encryption is when the entire file is read into memory, so it is very memory intensive. * WARRING:Encryption is when the entire file is read into memory, so it is very memory intensive.
*
*/ */
private String password; private String password;
/** /**

1
easyexcel-support/pom.xml

@ -11,6 +11,7 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<url>https://github.com/alibaba/easyexcel</url>
<packaging>jar</packaging> <packaging>jar</packaging>
<artifactId>easyexcel-support</artifactId> <artifactId>easyexcel-support</artifactId>
<name>easyexcel-support</name> <name>easyexcel-support</name>

5
easyexcel-test/pom.xml

@ -11,6 +11,7 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<url>https://github.com/alibaba/easyexcel</url>
<packaging>jar</packaging> <packaging>jar</packaging>
<artifactId>easyexcel-test</artifactId> <artifactId>easyexcel-test</artifactId>
<name>easyexcel-test</name> <name>easyexcel-test</name>
@ -31,8 +32,8 @@
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson2</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java

@ -12,7 +12,7 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java

@ -6,7 +6,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Assert; import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;

43
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java

@ -1,16 +1,24 @@
package com.alibaba.easyexcel.test.core.compatibility; package com.alibaba.easyexcel.test.core.compatibility;
import java.io.File;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.easyexcel.test.core.large.LargeData;
import com.alibaba.easyexcel.test.core.large.LargeDataListener;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.cache.Ehcache;
import com.alibaba.excel.constant.EasyExcelConstants; import com.alibaba.excel.constant.EasyExcelConstants;
import com.alibaba.excel.enums.ReadDefaultReturnEnum; import com.alibaba.excel.enums.ReadDefaultReturnEnum;
import com.alibaba.fastjson.JSON; import com.alibaba.excel.util.FileUtils;
import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.TempFile;
import org.junit.Assert; import org.junit.Assert;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
@ -116,4 +124,37 @@ public class CompatibilityTest {
Assert.assertEquals("24.20", list.get(0).get(11)); Assert.assertEquals("24.20", list.get(0).get(11));
} }
@Test
public void t08() {
// https://github.com/alibaba/easyexcel/issues/2693
// Temporary files may be deleted if there is no operation for a long time, so they need to be recreated.
File file = TestFileUtil.createNewFile("compatibility/t08.xlsx");
EasyExcel.write(file, SimpleData.class)
.sheet()
.doWrite(data());
List<Map<Integer, Object>> list = EasyExcel.read(file)
.readCache(new Ehcache(null, 20))
.sheet()
.doReadSync();
Assert.assertEquals(10L, list.size());
FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR)));
list = EasyExcel.read(file)
.readCache(new Ehcache(null, 20))
.sheet()
.doReadSync();
Assert.assertEquals(10L, list.size());
}
private List<SimpleData> data() {
List<SimpleData> list = new ArrayList<SimpleData>();
for (int i = 0; i < 10; i++) {
SimpleData simpleData = new SimpleData();
simpleData.setName("姓名" + i);
list.add(simpleData);
}
return list;
}
} }

13
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java

@ -3,14 +3,16 @@ package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.text.ParseException; import java.text.ParseException;
import java.time.LocalDate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestUtil;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Assert; import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -32,12 +34,9 @@ public class ConverterDataListener extends AnalysisEventListener<ConverterReadDa
public void doAfterAllAnalysed(AnalysisContext context) { public void doAfterAllAnalysed(AnalysisContext context) {
Assert.assertEquals(list.size(), 1); Assert.assertEquals(list.size(), 1);
ConverterReadData data = list.get(0); ConverterReadData data = list.get(0);
try { Assert.assertEquals(TestUtil.TEST_DATE, data.getDate());
Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate()); Assert.assertEquals(TestUtil.TEST_LOCAL_DATE, data.getLocalDate());
} catch (ParseException e) { Assert.assertEquals(TestUtil.TEST_LOCAL_DATE_TIME, data.getLocalDateTime());
throw new ExcelCommonException("Test Exception", e);
}
Assert.assertEquals(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null), data.getLocalDateTime());
Assert.assertEquals(data.getBooleanData(), Boolean.TRUE); Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0); Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0); Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);

8
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataTest.java

@ -4,10 +4,13 @@ import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.easyexcel.test.util.TestUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.WriteCellData; import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
@ -112,8 +115,9 @@ public class ConverterDataTest {
private List<ConverterWriteData> data() throws Exception { private List<ConverterWriteData> data() throws Exception {
List<ConverterWriteData> list = new ArrayList<ConverterWriteData>(); List<ConverterWriteData> list = new ArrayList<ConverterWriteData>();
ConverterWriteData converterWriteData = new ConverterWriteData(); ConverterWriteData converterWriteData = new ConverterWriteData();
converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); converterWriteData.setDate(TestUtil.TEST_DATE);
converterWriteData.setLocalDateTime(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null)); converterWriteData.setLocalDate(TestUtil.TEST_LOCAL_DATE);
converterWriteData.setLocalDateTime(TestUtil.TEST_LOCAL_DATE_TIME);
converterWriteData.setBooleanData(Boolean.TRUE); converterWriteData.setBooleanData(Boolean.TRUE);
converterWriteData.setBigDecimal(BigDecimal.ONE); converterWriteData.setBigDecimal(BigDecimal.ONE);
converterWriteData.setBigInteger(BigInteger.ONE); converterWriteData.setBigInteger(BigInteger.ONE);

3
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java

@ -2,6 +2,7 @@ package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@ -22,6 +23,8 @@ public class ConverterReadData {
@ExcelProperty("日期") @ExcelProperty("日期")
private Date date; private Date date;
@ExcelProperty("本地日期") @ExcelProperty("本地日期")
private LocalDate localDate;
@ExcelProperty("本地日期时间")
private LocalDateTime localDateTime; private LocalDateTime localDateTime;
@ExcelProperty("布尔") @ExcelProperty("布尔")
private Boolean booleanData; private Boolean booleanData;

3
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java

@ -2,6 +2,7 @@ package com.alibaba.easyexcel.test.core.converter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.BigInteger; import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
@ -22,6 +23,8 @@ public class ConverterWriteData {
@ExcelProperty("日期") @ExcelProperty("日期")
private Date date; private Date date;
@ExcelProperty("本地日期") @ExcelProperty("本地日期")
private LocalDate localDate;
@ExcelProperty("本地日期时间")
private LocalDateTime localDateTime; private LocalDateTime localDateTime;
@ExcelProperty("布尔") @ExcelProperty("布尔")
private Boolean booleanData; private Boolean booleanData;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java

@ -11,7 +11,7 @@ import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.exception.ExcelCommonException;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.DateUtils; import com.alibaba.excel.util.DateUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Assert; import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java

@ -14,7 +14,7 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.enums.CellExtraTypeEnum; import com.alibaba.excel.enums.CellExtraTypeEnum;
import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java

@ -7,7 +7,7 @@ import java.util.Map;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Assert; import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java

@ -3,7 +3,7 @@ package com.alibaba.easyexcel.test.core.large;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Assert; import org.junit.Assert;
import org.slf4j.Logger; import org.slf4j.Logger;

6
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java

@ -2,14 +2,19 @@ package com.alibaba.easyexcel.test.core.large;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.cache.Ehcache;
import com.alibaba.excel.cache.ReadCache;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.util.TempFile;
import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFSheet;
@ -55,6 +60,7 @@ public class LargeDataTest {
LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start);
} }
@Test @Test
public void t02Fill() { public void t02Fill() {
try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) { try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) {

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java

@ -6,7 +6,7 @@ import java.util.Map;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java

@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.easyexcel.test.core.simple.SimpleDataListener;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java

@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

21
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java

@ -5,11 +5,15 @@ import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import com.alibaba.easyexcel.test.demo.read.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
@ -20,6 +24,7 @@ import org.junit.runners.MethodSorters;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@Slf4j
public class SimpleDataTest { public class SimpleDataTest {
private static File file07; private static File file07;
@ -90,8 +95,20 @@ public class SimpleDataTest {
@Test @Test
public void t21SheetNameRead07() { public void t21SheetNameRead07() {
EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, List<Map<Integer, Object>> list = EasyExcel.read(
new SimpleDataSheetNameListener()).sheet("simple").doRead(); TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"))
.sheet("simple")
.doReadSync();
Assert.assertEquals(1, list.size());
}
@Test
public void t22PageReadListener07() {
EasyExcel.read(file07, SimpleData.class,
new PageReadListener<SimpleData>(dataList -> {
Assert.assertEquals(5, dataList.size());
}, 5))
.sheet().doRead();
} }
private void synchronousRead(File file) { private void synchronousRead(File file) {

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java

@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.easyexcel.test.core.simple.SimpleDataListener;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java

@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener; import com.alibaba.easyexcel.test.core.simple.SimpleDataListener;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

1
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java

@ -64,6 +64,7 @@ public class FillTest {
public void listFill() { public void listFill() {
// 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
// 填充list 的时候还要注意 模板中{.} 多了个点 表示list // 填充list 的时候还要注意 模板中{.} 多了个点 表示list
// 如果填充list的对象是map,必须包涵所有list的key,哪怕数据为null,必须使用map.put(key,null)
String templateFileName = String templateFileName =
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx"; TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx";

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java

@ -5,7 +5,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java

@ -5,7 +5,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java

@ -5,7 +5,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java

@ -8,7 +8,7 @@ import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

4
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java

@ -3,7 +3,7 @@ package com.alibaba.easyexcel.test.demo.read;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.CellExtra; import com.alibaba.excel.metadata.CellExtra;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;
@ -46,7 +46,7 @@ public class DemoExtraListener implements ReadListener<DemoExtraData> {
break; break;
case MERGE: case MERGE:
log.info( log.info(
"额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", "额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}",
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
extra.getLastColumnIndex()); extra.getLastColumnIndex());
break; break;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java

@ -8,7 +8,7 @@ import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java

@ -5,7 +5,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java

@ -6,7 +6,7 @@ import java.util.Map;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

5
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java

@ -17,7 +17,7 @@ import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore; import org.junit.Ignore;
@ -47,7 +47,8 @@ public class ReadTest {
// since: 3.0.0-beta1 // since: 3.0.0-beta1
String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
// 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行 // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行
// 具体需要返回多少行可以在`PageReadListener`的构造函数设置
EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> { EasyExcel.read(fileName, DemoData.class, new PageReadListener<DemoData>(dataList -> {
for (DemoData demoData : dataList) { for (DemoData demoData : dataList) {
log.info("读取到一条数据{}", JSON.toJSONString(demoData)); log.info("读取到一条数据{}", JSON.toJSONString(demoData));

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java

@ -5,7 +5,7 @@ import java.util.List;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java

@ -11,7 +11,7 @@ import javax.servlet.http.HttpServletResponse;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.util.MapUtils; import com.alibaba.excel.util.MapUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;

7
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java

@ -242,6 +242,10 @@ public class WriteTest {
public void imageWrite() throws Exception { public void imageWrite() throws Exception {
String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
// 这里注意下 所有的图片都会放到内存 暂时没有很好的解法,大量图片的情况下建议 2选1:
// 1. 将图片上传到oss 或者其他存储网站: https://www.aliyun.com/product/oss ,然后直接放链接
// 2. 使用: https://github.com/coobird/thumbnailator 或者其他工具压缩图片
String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg"; String imagePath = TestFileUtil.getPath() + "converter" + File.separator + "img.jpg";
try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) { try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) {
List<ImageDemoData> list = ListUtils.newArrayList(); List<ImageDemoData> list = ListUtils.newArrayList();
@ -361,6 +365,7 @@ public class WriteTest {
writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex()); writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex());
// 设置单个单元格多种样式 // 设置单个单元格多种样式
// 这里需要设置 inMomery=true 不然会导致无法展示单个单元格多种样式,所以慎用
WriteCellData<String> richTest = new WriteCellData<>(); WriteCellData<String> richTest = new WriteCellData<>();
richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING); richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING);
writeCellDemoData.setRichText(richTest); writeCellDemoData.setRichText(richTest);
@ -397,6 +402,8 @@ public class WriteTest {
String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx";
String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx"; String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx";
// 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
// 这里要注意 withTemplate 的模板文件会全量存储在内存里面,所以尽量不要用于追加文件,如果文件模板文件过大会OOM
// 如果要再文件中追加(无法在一个线程里面处理,可以在一个线程的建议参照多次写入的demo) 建议临时存储到数据库 或者 磁盘缓存(ehcache) 然后再一次性写入
EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data()); EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data());
} }

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java

@ -24,7 +24,7 @@ import com.alibaba.excel.util.PositionUtils;
import com.alibaba.excel.write.metadata.style.WriteCellStyle; import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont; import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java

@ -9,7 +9,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.demo.read.DemoDataListener; import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 模板的读取类 * 模板的读取类

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java

@ -5,7 +5,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java

@ -28,7 +28,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 临时测试 * 临时测试

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java

@ -4,7 +4,7 @@ import java.util.List;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java vendored

@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.easyexcel.test.temp.poi.Poi2Test; import com.alibaba.easyexcel.test.temp.poi.Poi2Test;
import com.alibaba.excel.util.FileUtils; import com.alibaba.excel.util.FileUtils;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* *

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java

@ -2,7 +2,7 @@ package com.alibaba.easyexcel.test.temp.csv;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java

@ -11,7 +11,7 @@ import java.util.List;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java

@ -13,7 +13,7 @@ import com.alibaba.easyexcel.test.core.dataformat.DateFormatData;
import com.alibaba.easyexcel.test.temp.Lock2Test; import com.alibaba.easyexcel.test.temp.Lock2Test;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Cell;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java

@ -5,7 +5,7 @@ import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.NumberUtils; import com.alibaba.excel.util.NumberUtils;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java

@ -5,7 +5,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* @author Jiaju Zhuang * @author Jiaju Zhuang

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java

@ -8,7 +8,7 @@ import java.math.BigDecimal;
import java.net.URL; import java.net.URL;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFRow;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java

@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.CellData; import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 临时测试 * 临时测试

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 模板的读取类 * 模板的读取类

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java

@ -7,7 +7,7 @@ import org.slf4j.LoggerFactory;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 模板的读取类 * 模板的读取类

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/TestListener.java

@ -2,7 +2,7 @@ package com.alibaba.easyexcel.test.temp.read;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgListener.java

@ -4,7 +4,7 @@ import java.util.Map;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java

@ -12,7 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 测试poi * 测试poi

2
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java

@ -10,7 +10,7 @@ import com.alibaba.easyexcel.test.demo.read.DemoDataListener;
import com.alibaba.easyexcel.test.temp.LockData; import com.alibaba.easyexcel.test.temp.LockData;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
/** /**
* 模板的读取类 * 模板的读取类

6
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java → easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java

@ -13,7 +13,7 @@ import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable; import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Ignore; import org.junit.Ignore;
@ -28,8 +28,8 @@ import org.slf4j.LoggerFactory;
**/ **/
@Ignore @Ignore
@Slf4j @Slf4j
public class Wirte { public class Write {
private static final Logger LOGGER = LoggerFactory.getLogger(Wirte.class); private static final Logger LOGGER = LoggerFactory.getLogger(Write.class);
@Test @Test
public void simpleWrite1() { public void simpleWrite1() {

126
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java

@ -1,15 +1,47 @@
package com.alibaba.easyexcel.test.temp.write; package com.alibaba.easyexcel.test.temp.write;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.easyexcel.test.demo.read.CustomStringStringConverter; import com.alibaba.easyexcel.test.demo.read.CustomStringStringConverter;
import com.alibaba.easyexcel.test.demo.write.DemoData;
import com.alibaba.easyexcel.test.demo.write.ImageDemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.ImageData;
import com.alibaba.excel.metadata.data.ImageData.ImageType;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.BeanMapUtils;
import com.alibaba.excel.util.FileUtils;
import com.alibaba.excel.util.ListUtils; import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFDrawing;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.springframework.cglib.beans.BeanMap; import org.springframework.cglib.beans.BeanMap;
@ -28,7 +60,6 @@ public class TempWriteTest {
.registerConverter(new CustomStringStringConverter()) .registerConverter(new CustomStringStringConverter())
.doWrite(ListUtils.newArrayList(tempWriteData)); .doWrite(ListUtils.newArrayList(tempWriteData));
EasyExcel.write(TestFileUtil.getPath() + "TempWriteTest" + System.currentTimeMillis() + ".xlsx", EasyExcel.write(TestFileUtil.getPath() + "TempWriteTest" + System.currentTimeMillis() + ".xlsx",
TempWriteData.class) TempWriteData.class)
.sheet() .sheet()
@ -55,4 +86,97 @@ public class TempWriteTest {
log.info("3{}", tempWriteData2.getName()); log.info("3{}", tempWriteData2.getName());
} }
@Test
public void imageWrite() throws Exception {
//String fileName = TestFileUtil.getPath() + "imageWrite" + System.currentTimeMillis() + ".xlsx";
//
//// 这里 需要指定写用哪个class去写
//try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
// // 这里注意 如果同一个sheet只要创建一次
// WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
// // 去调用写入,这里我调用了五次,实际使用时根据数据库分页的总的页数来
// for (int i = 0; i < 5; i++) {
// // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
// List<DemoData> data = data();
// excelWriter.write(data, writeSheet);
// }
//}
}
@Test
public void imageWritePoi() throws Exception {
String file = "/Users/zhuangjiaju/test/imagetest" + System.currentTimeMillis() + ".xlsx";
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("测试");
CreationHelper helper = workbook.getCreationHelper();
SXSSFDrawing sxssfDrawin = sheet.createDrawingPatriarch();
byte[] imagebyte = FileUtils.readFileToByteArray(new File("/Users/zhuangjiaju/Documents/demo.jpg"));
for (int i = 0; i < 1 * 10000; i++) {
SXSSFRow row = sheet.createRow(i);
SXSSFCell cell = row.createCell(0);
cell.setCellValue(i);
int pictureIdx = workbook.addPicture(imagebyte, Workbook.PICTURE_TYPE_JPEG);
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0);
anchor.setRow1(i);
// 插入图片
Picture pict = sxssfDrawin.createPicture(anchor, pictureIdx);
pict.resize();
log.info("新增行:{}", i);
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
fileOutputStream.flush();
workbook.close();
}
@Test
public void tep() throws Exception {
String file = "/Users/zhuangjiaju/test/imagetest" + System.currentTimeMillis() + ".xlsx";
SXSSFWorkbook workbook = new SXSSFWorkbook();
SXSSFSheet sheet = workbook.createSheet("测试");
CreationHelper helper = workbook.getCreationHelper();
SXSSFDrawing sxssfDrawin = sheet.createDrawingPatriarch();
byte[] imagebyte = FileUtils.readFileToByteArray(new File("/Users/zhuangjiaju/Documents/demo.jpg"));
for (int i = 0; i < 1 * 10000; i++) {
SXSSFRow row = sheet.createRow(i);
SXSSFCell cell = row.createCell(0);
cell.setCellValue(i);
int pictureIdx = workbook.addPicture(imagebyte, Workbook.PICTURE_TYPE_JPEG);
ClientAnchor anchor = helper.createClientAnchor();
anchor.setCol1(0);
anchor.setRow1(i);
// 插入图片
Picture pict = sxssfDrawin.createPicture(anchor, pictureIdx);
pict.resize();
log.info("新增行:{}", i);
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
fileOutputStream.flush();
workbook.close();
}
@Test
public void large() throws Exception {
String file = "/Users/zhuangjiaju/test/imagetest" + System.currentTimeMillis() + ".xlsx";
SXSSFWorkbook workbook = new SXSSFWorkbook(new XSSFWorkbook(
new File("/Users/zhuangjiaju/IdeaProjects/easyexcel/easyexcel-test/src/test/resources/large/large07.xlsx")));
SXSSFSheet sheet = workbook.createSheet("测试");
SXSSFRow row = sheet.createRow(500000);
SXSSFCell cell = row.createCell(0);
cell.setCellValue("test");
FileOutputStream fileOutputStream = new FileOutputStream(file);
workbook.write(fileOutputStream);
fileOutputStream.flush();
workbook.close();
}
} }

36
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java

@ -0,0 +1,36 @@
package com.alibaba.easyexcel.test.util;
import java.io.File;
import java.io.InputStream;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import com.alibaba.excel.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
/**
* test util
*
* @author Jiaju Zhuang
*/
@Slf4j
public class TestUtil {
public static final Date TEST_DATE;
public static final LocalDate TEST_LOCAL_DATE = LocalDate.of(2020, 1, 1);
public static final LocalDateTime TEST_LOCAL_DATE_TIME = LocalDateTime.of(2020, 1, 1, 1, 1, 1);
static {
try {
TEST_DATE = DateUtils.parseDate("2020-01-01 01:01:01");
} catch (ParseException e) {
log.error("init TestUtil error.", e);
throw new RuntimeException(e);
}
}
}

1
easyexcel/pom.xml

@ -11,6 +11,7 @@
<relativePath>../pom.xml</relativePath> <relativePath>../pom.xml</relativePath>
</parent> </parent>
<url>https://github.com/alibaba/easyexcel</url>
<packaging>jar</packaging> <packaging>jar</packaging>
<artifactId>easyexcel</artifactId> <artifactId>easyexcel</artifactId>
<name>easyexcel</name> <name>easyexcel</name>

BIN
img/readme/alipay.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 22 KiB

BIN
img/readme/large.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 57 KiB

After

Width:  |  Height:  |  Size: 20 KiB

8
pom.xml

@ -20,7 +20,7 @@
<properties> <properties>
<revision>3.2.0</revision> <revision>3.2.1</revision>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.8</jdk.version> <jdk.version>1.8</jdk.version>
<gpg.skip>true</gpg.skip> <gpg.skip>true</gpg.skip>
@ -143,9 +143,9 @@
<version>1.2.5</version> <version>1.2.5</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.alibaba</groupId> <groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson</artifactId> <artifactId>fastjson2</artifactId>
<version>2.0.6</version> <version>2.0.23</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

10
update.md

@ -1,3 +1,10 @@
# 3.2.1
* 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908)
* 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657)
* 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693)
* 分页读取监听器支持自定义分页条数 [Issue #2383](https://github.com/alibaba/easyexcel/issues/2383)
# 3.2.0 # 3.2.0
* 修复部分xlsx读取日期可能相差1秒的bug [Issue #1956](https://github.com/alibaba/easyexcel/issues/1956) * 修复部分xlsx读取日期可能相差1秒的bug [Issue #1956](https://github.com/alibaba/easyexcel/issues/1956)
@ -10,7 +17,8 @@
# 3.1.4 # 3.1.4
* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列 * 提高xlsx读取兼容性:在存在第一行很多空
* 列的情况下,忽略空列
# 3.1.3 # 3.1.3

Loading…
Cancel
Save