diff --git a/.github/ISSUE_TEMPLATE/bug.md b/.github/ISSUE_TEMPLATE/bug.md index 7ef28dd1..f30eacb5 100644 --- a/.github/ISSUE_TEMPLATE/bug.md +++ b/.github/ISSUE_TEMPLATE/bug.md @@ -16,3 +16,5 @@ assignees: zhuangjiaju 这里写代码 ``` # 提示的异常或者没有达到的效果 +大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 +至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。 diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md index bd234cf8..daf6eacd 100644 --- a/.github/ISSUE_TEMPLATE/question.md +++ b/.github/ISSUE_TEMPLATE/question.md @@ -15,4 +15,5 @@ assignees: '' ``` # 异常提示 大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。 +至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。 # 问题描述 diff --git a/.github/workflows/sync2gitee.yml b/.github/workflows/sync2gitee.yml index cc88d581..5b48b08b 100644 --- a/.github/workflows/sync2gitee.yml +++ b/.github/workflows/sync2gitee.yml @@ -1,6 +1,6 @@ # 通过 Github action, 在仓库的每一次 commit 后自动同步到 Gitee 上 name: Mirror the Github organization repos to Gitee -on: [push, pull_request] +on: [push] jobs: repo-sync: diff --git a/README.md b/README.md index 9586e7b3..004f12c7 100644 --- a/README.md +++ b/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) * 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) 能更快,但是内存占用会在100M多一点 @@ -28,7 +28,7 @@ easyexcel重写了poi对07版Excel的解析,一个3M的excel用POI sax解析 com.alibaba easyexcel - 3.2.0 + 3.2.1 ``` diff --git a/easyexcel-core/pom.xml b/easyexcel-core/pom.xml index d6e58002..9cfabb2a 100644 --- a/easyexcel-core/pom.xml +++ b/easyexcel-core/pom.xml @@ -3,7 +3,6 @@ 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"> 4.0.0 - com.alibaba easyexcel-parent @@ -11,6 +10,7 @@ ../pom.xml + https://github.com/alibaba/easyexcel jar easyexcel-core easyexcel-core diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java index 9508d941..83b46f40 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java +++ b/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.ReadWorkbook; +import lombok.extern.slf4j.Slf4j; + /** * Excel readers are all read in event mode. * * @author jipengfei */ +@Slf4j public class ExcelReader implements Closeable { /** @@ -104,4 +107,17 @@ public class ExcelReader implements Closeable { public void close() { 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); + } + } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java index 95d5446b..3c074384 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/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.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 * functions. @@ -23,6 +25,7 @@ import com.alibaba.excel.write.metadata.fill.FillConfig; * * @author jipengfei */ +@Slf4j public class ExcelWriter implements Closeable { private final ExcelBuilder excelBuilder; @@ -154,4 +157,16 @@ public class ExcelWriter implements Closeable { public void close() { 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); + } + } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java index 685dcc65..ad475522 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/handlers/BoundSheetRecordHandler.java @@ -16,6 +16,6 @@ public class BoundSheetRecordHandler extends AbstractXlsRecordHandler implements @Override public void processRecord(XlsReadContext xlsReadContext, Record record) { BoundSheetRecord bsr = (BoundSheetRecord)record; - xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add((BoundSheetRecord)record); + xlsReadContext.xlsReadWorkbookHolder().getBoundSheetRecordList().add(bsr); } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java index b7b542ee..6387d2a1 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java @@ -2,6 +2,7 @@ package com.alibaba.excel.cache; import java.io.File; import java.util.ArrayList; +import java.util.Optional; import java.util.UUID; 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.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.EntryUnit; 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 CacheConfiguration FILE_CACHE_CONFIGURATION; private static final CacheManager ACTIVE_CACHE_MANAGER; + private static final File CACHE_PATH_FILE; + private final CacheConfiguration activeCacheConfiguration; /** * Bulk storage data @@ -50,29 +54,61 @@ public class Ehcache implements ReadCache { */ private int cacheMiss = 0; - public Ehcache(int maxCacheActivateSize) { - activeCacheConfiguration = CacheConfigurationBuilder - .newCacheConfigurationBuilder(Integer.class, ArrayList.class, - ResourcePoolsBuilder.newResourcePoolsBuilder().heap(maxCacheActivateSize, MemoryUnit.MB)) - .withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(maxCacheActivateSize, MemoryUnit.MB) - .build(); + @Deprecated + 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, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(maxCacheActivateSize, MemoryUnit.MB)) + .build(); + } else { + this.activeCacheConfiguration = CacheConfigurationBuilder + .newCacheConfigurationBuilder(Integer.class, ArrayList.class, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(maxCacheActivateBatchCount, EntryUnit.ENTRIES)) + .build(); + } } static { - File cacheFile = FileUtils.createCacheTmpFile(); + CACHE_PATH_FILE = FileUtils.createCacheTmpFile(); 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); FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder - .newCacheConfigurationBuilder(Integer.class, ArrayList.class, - ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) - .withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build(); + .newCacheConfigurationBuilder(Integer.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder() + .disk(20, MemoryUnit.GB)).build(); } @Override public void init(AnalysisContext analysisContext) { cacheAlias = UUID.randomUUID().toString(); - fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION); + try { + 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); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java index 658806bd..278c63be 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java @@ -2,6 +2,10 @@ package com.alibaba.excel.cache.selector; 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.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -15,6 +19,9 @@ import com.alibaba.excel.cache.ReadCache; * * @author Jiaju Zhuang **/ +@Getter +@Setter +@EqualsAndHashCode public class SimpleReadCacheSelector implements ReadCacheSelector { private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class); /** @@ -24,37 +31,46 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { /** * 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. */ - private final long maxUseMapCacheSize; + private Long maxUseMapCacheSize; /** * 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() { - this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE); } - public SimpleReadCacheSelector(long maxUseMapCacheSize, int maxCacheActivateSize) { - if (maxUseMapCacheSize <= 0) { - this.maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE; - } else { - this.maxUseMapCacheSize = maxUseMapCacheSize; - } - if (maxCacheActivateSize <= 0) { - this.maxCacheActivateSize = DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE; - } else { - this.maxCacheActivateSize = maxCacheActivateSize; - } + /** + * Parameter maxCacheActivateSize has already been abandoned + * + * @param maxUseMapCacheSize + * @param maxCacheActivateSize + */ + @Deprecated + public SimpleReadCacheSelector(Long maxUseMapCacheSize, Integer maxCacheActivateSize) { + this.maxUseMapCacheSize = maxUseMapCacheSize; + this.maxCacheActivateSize = maxCacheActivateSize; } @Override @@ -68,6 +84,9 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { return new MapCache(); } } + if (maxUseMapCacheSize == null) { + maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE; + } if (size < maxUseMapCacheSize * B2M) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Use map cache.size:{}", size); @@ -77,6 +96,17 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { if (LOGGER.isDebugEnabled()) { 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); + } + } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java index ac2dd8dc..1b04d240 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/converters/DefaultConverterLoader.java +++ b/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.IntegerNumberConverter; 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.LocalDateTimeStringConverter; import com.alibaba.excel.converters.longconverter.LongBooleanConverter; @@ -83,6 +86,9 @@ public class DefaultConverterLoader { putAllConverter(new DateStringConverter()); putAllConverter(new LocalDateNumberConverter()); + putAllConverter(new LocalDateStringConverter()); + + putAllConverter(new LocalDateTimeNumberConverter()); putAllConverter(new LocalDateTimeStringConverter()); putAllConverter(new DoubleBooleanConverter()); @@ -121,6 +127,7 @@ public class DefaultConverterLoader { putWriteConverter(new ByteNumberConverter()); putWriteConverter(new DateDateConverter()); putWriteConverter(new LocalDateTimeDateConverter()); + putWriteConverter(new LocalDateDateConverter()); putWriteConverter(new DoubleNumberConverter()); putWriteConverter(new FloatNumberConverter()); putWriteConverter(new IntegerNumberConverter()); @@ -139,6 +146,7 @@ public class DefaultConverterLoader { putWriteStringConverter(new BooleanStringConverter()); putWriteStringConverter(new ByteStringConverter()); putWriteStringConverter(new DateStringConverter()); + putWriteStringConverter(new LocalDateStringConverter()); putWriteStringConverter(new LocalDateTimeStringConverter()); putWriteStringConverter(new DoubleStringConverter()); putWriteStringConverter(new FloatStringConverter()); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateDateConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateDateConverter.java new file mode 100644 index 00000000..3663476a --- /dev/null +++ b/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 { + @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; + } +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateNumberConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateNumberConverter.java new file mode 100644 index 00000000..fbec7a4f --- /dev/null +++ b/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 { + + @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()))); + } + } +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateStringConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdate/LocalDateStringConverter.java new file mode 100644 index 00000000..68e3e0b0 --- /dev/null +++ b/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 { + @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())); + } + } +} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java index 072a7864..39276df9 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeDateConverter.java +++ b/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; /** - * Date and date converter + * LocalDateTime and date converter * * @author Jiaju Zhuang */ public class LocalDateTimeDateConverter implements Converter { @Override - public Class supportJavaTypeKey() { + public Class supportJavaTypeKey() { return LocalDateTime.class; } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java b/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeNumberConverter.java similarity index 96% rename from easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java rename to easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateTimeNumberConverter.java index 4542fd08..51959fde 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/converters/localdatetime/LocalDateNumberConverter.java +++ b/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 */ -public class LocalDateNumberConverter implements Converter { +public class LocalDateTimeNumberConverter implements Converter { @Override public Class supportJavaTypeKey() { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java b/easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java index f8ce9064..d4345fa9 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/metadata/data/WriteCellData.java +++ b/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; /** - * wirte cell data + * write cell data * * @author Jiaju Zhuang */ diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java index becc1748..b97db357 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/listener/PageReadListener.java +++ b/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 implements ReadListener { /** - * Single handle the amount of data + * Defuault single handle the amount of data */ public static int BATCH_COUNT = 100; /** @@ -27,16 +27,26 @@ public class PageReadListener implements ReadListener { */ private final Consumer> consumer; + /** + * Single handle the amount of data + */ + private final int batchCount; + public PageReadListener(Consumer> consumer) { + this(consumer, BATCH_COUNT); + } + + public PageReadListener(Consumer> consumer, int batchCount) { this.consumer = consumer; + this.batchCount = batchCount; } @Override public void invoke(T data, AnalysisContext context) { cachedDataList.add(data); - if (cachedDataList.size() >= BATCH_COUNT) { + if (cachedDataList.size() >= batchCount) { consumer.accept(cachedDataList); - cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + cachedDataList = ListUtils.newArrayListWithExpectedSize(batchCount); } } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java index fb19a105..cab7e4d0 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/DateUtils.java +++ b/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.ParseException; import java.text.SimpleDateFormat; +import java.time.LocalDate; import java.time.LocalDateTime; +import java.time.LocalTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; @@ -59,6 +61,8 @@ public class DateUtils { public static String defaultDateFormat = DATE_FORMAT_19; + public static String defaultLocalDateFormat = DATE_FORMAT_10; + 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 * @@ -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 * @@ -271,6 +326,25 @@ public class DateUtils { 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. * diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java index 881c1437..d800be51 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/util/FileUtils.java @@ -174,7 +174,7 @@ public class FileUtils { * * @param directory */ - private static File createDirectory(File directory) { + public static File createDirectory(File directory) { if (!directory.exists() && !directory.mkdirs()) { throw new ExcelCommonException("Cannot create directory:" + directory.getAbsolutePath()); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java b/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java index c2c727c7..12886c85 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/util/SheetUtils.java +++ b/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 * - * @param readSheet - * actual sheet + * @param readSheet actual sheet * @param analysisContext * @return */ @@ -47,11 +46,13 @@ public class SheetUtils { if (!StringUtils.isEmpty(parameterSheetName)) { boolean autoTrim = (parameterReadSheet.getAutoTrim() != null && parameterReadSheet.getAutoTrim()) || (parameterReadSheet.getAutoTrim() == null - && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); + && analysisContext.readWorkbookHolder().getGlobalConfiguration().getAutoTrim()); + String sheetName = readSheet.getSheetName(); if (autoTrim) { parameterSheetName = parameterSheetName.trim(); + sheetName = sheetName.trim(); } - match = parameterSheetName.equals(readSheet.getSheetName()); + match = parameterSheetName.equals(sheetName); } } if (match) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index 75d702f8..76af6f66 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java @@ -66,7 +66,6 @@ public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder * If 'inputStream' and 'file' all not empty, file first */ @@ -66,7 +67,6 @@ public class WriteWorkbook extends WriteBasicParameter { * Whether the encryption *

* WARRING:Encryption is when the entire file is read into memory, so it is very memory intensive. - * */ private String password; /** diff --git a/easyexcel-support/pom.xml b/easyexcel-support/pom.xml index db2dbd3f..23613040 100644 --- a/easyexcel-support/pom.xml +++ b/easyexcel-support/pom.xml @@ -11,6 +11,7 @@ ../pom.xml + https://github.com/alibaba/easyexcel jar easyexcel-support easyexcel-support diff --git a/easyexcel-test/pom.xml b/easyexcel-test/pom.xml index 80821db2..73fc20d7 100644 --- a/easyexcel-test/pom.xml +++ b/easyexcel-test/pom.xml @@ -11,6 +11,7 @@ ../pom.xml + https://github.com/alibaba/easyexcel jar easyexcel-test easyexcel-test @@ -31,8 +32,8 @@ test - com.alibaba - fastjson + com.alibaba.fastjson2 + fastjson2 test diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java index 09bfc523..222d83b5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationDataListener.java +++ b/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.exception.ExcelCommonException; import com.alibaba.excel.util.DateUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java index 115b1b03..ea091f23 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/annotation/AnnotationIndexAndNameDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java index 35829e0e..acafba47 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/celldata/CellDataDataListener.java +++ b/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.event.AnalysisEventListener; import com.alibaba.excel.support.ExcelTypeEnum; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Assert; import org.slf4j.Logger; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java index f4aae4a8..df5a2dbb 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java @@ -1,16 +1,24 @@ package com.alibaba.easyexcel.test.core.compatibility; +import java.io.File; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; 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.excel.EasyExcel; +import com.alibaba.excel.cache.Ehcache; import com.alibaba.excel.constant.EasyExcelConstants; 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 org.apache.poi.util.TempFile; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; @@ -116,4 +124,37 @@ public class CompatibilityTest { 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> 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 data() { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + SimpleData simpleData = new SimpleData(); + simpleData.setName("姓名" + i); + list.add(simpleData); + } + return list; + } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index fc353ba2..a7327d42 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/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.BigInteger; import java.text.ParseException; +import java.time.LocalDate; import java.util.ArrayList; import java.util.List; +import com.alibaba.easyexcel.test.util.TestUtil; import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; import com.alibaba.excel.exception.ExcelCommonException; import com.alibaba.excel.util.DateUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Assert; import org.slf4j.Logger; @@ -32,12 +34,9 @@ public class ConverterDataListener extends AnalysisEventListener data() throws Exception { List list = new ArrayList(); ConverterWriteData converterWriteData = new ConverterWriteData(); - converterWriteData.setDate(DateUtils.parseDate("2020-01-01 01:01:01")); - converterWriteData.setLocalDateTime(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null)); + converterWriteData.setDate(TestUtil.TEST_DATE); + converterWriteData.setLocalDate(TestUtil.TEST_LOCAL_DATE); + converterWriteData.setLocalDateTime(TestUtil.TEST_LOCAL_DATE_TIME); converterWriteData.setBooleanData(Boolean.TRUE); converterWriteData.setBigDecimal(BigDecimal.ONE); converterWriteData.setBigInteger(BigInteger.ONE); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java index f9a6bee6..1ee3aea9 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterReadData.java +++ b/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.BigInteger; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; @@ -22,6 +23,8 @@ public class ConverterReadData { @ExcelProperty("日期") private Date date; @ExcelProperty("本地日期") + private LocalDate localDate; + @ExcelProperty("本地日期时间") private LocalDateTime localDateTime; @ExcelProperty("布尔") private Boolean booleanData; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java index 6782aa1a..88d7b765 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterWriteData.java +++ b/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.BigInteger; +import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; @@ -22,6 +23,8 @@ public class ConverterWriteData { @ExcelProperty("日期") private Date date; @ExcelProperty("本地日期") + private LocalDate localDate; + @ExcelProperty("本地日期时间") private LocalDateTime localDateTime; @ExcelProperty("布尔") private Boolean booleanData; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java index 8516cd5b..f56eab46 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ReadAllConverterDataListener.java +++ b/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.support.ExcelTypeEnum; import com.alibaba.excel.util.DateUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Assert; import org.slf4j.Logger; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataListener.java index 0fb603fb..545a5894 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/encrypt/EncryptDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java index d9c26e26..8009d4db 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/exception/ExceptionDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java index 9039ab06..d58c4cdb 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataListener.java +++ b/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.event.AnalysisEventListener; import com.alibaba.excel.metadata.CellExtra; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java index 4475d83c..92f35574 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/extra/ExtraDataTest.java +++ b/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.metadata.CellExtra; import com.alibaba.excel.read.listener.ReadListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java index a3441d5e..1eab8585 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ComplexDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java index 58660c90..3e0ba094 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/ListHeadDataListener.java +++ b/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.metadata.data.ReadCellData; import com.alibaba.excel.read.listener.ReadListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Assert; import org.slf4j.Logger; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java index f09c899b..ae07ab41 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/head/NoHeadDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java index dafe25fa..fd056a5c 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataListener.java +++ b/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.event.AnalysisEventListener; import com.alibaba.excel.support.ExcelTypeEnum; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Assert; import org.slf4j.Logger; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index d8acbba5..8862fe18 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/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.FileOutputStream; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; 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 org.apache.poi.util.TempFile; import org.apache.poi.xssf.streaming.SXSSFCell; import org.apache.poi.xssf.streaming.SXSSFRow; import org.apache.poi.xssf.streaming.SXSSFSheet; @@ -55,6 +60,7 @@ public class LargeDataTest { LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); } + @Test public void t02Fill() { try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java index d7b41582..c078f20e 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java index b307ddb4..8c5e93c7 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/noncamel/UnCamelDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java index 57cca660..f465e92c 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/parameter/ParameterDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java index ff0a332e..4827f67a 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataListener.java +++ b/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.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java index b8925f81..9dc708ae 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java index 72c4c151..59cfa64f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataSheetNameListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java index 80277545..236f0571 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/simple/SimpleDataTest.java +++ b/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.util.ArrayList; 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.excel.EasyExcel; +import com.alibaba.excel.read.listener.PageReadListener; import com.alibaba.excel.support.ExcelTypeEnum; +import lombok.extern.slf4j.Slf4j; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.FixMethodOrder; @@ -20,6 +24,7 @@ import org.junit.runners.MethodSorters; * @author Jiaju Zhuang */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) +@Slf4j public class SimpleDataTest { private static File file07; @@ -90,8 +95,20 @@ public class SimpleDataTest { @Test public void t21SheetNameRead07() { - EasyExcel.read(TestFileUtil.readFile("simple" + File.separator + "simple07.xlsx"), SimpleData.class, - new SimpleDataSheetNameListener()).sheet("simple").doRead(); + List> list = EasyExcel.read( + 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(dataList -> { + Assert.assertEquals(5, dataList.size()); + }, 5)) + .sheet().doRead(); } private void synchronousRead(File file) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java index 9246a10b..ee16e023 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/style/StyleDataListener.java +++ b/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.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java index 0286e2e3..e3b8b3cf 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/template/TemplateDataListener.java +++ b/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.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java index 618a030f..3b3c37fc 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/fill/FillTest.java @@ -64,6 +64,7 @@ public class FillTest { public void listFill() { // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替 // 填充list 的时候还要注意 模板中{.} 多了个点 表示list + // 如果填充list的对象是map,必须包涵所有list的key,哪怕数据为null,必须使用map.put(key,null) String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "list.xlsx"; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java index b1ef3d7e..e04bc307 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/CellDataDemoHeadDataListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java index 9dabbc86..b7d144a5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ConverterDataListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java index 80d27b26..a6ddaf95 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoDataListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java index 26d1c8de..498725a0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExceptionListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java index e9dcc199..130d5675 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoExtraListener.java +++ b/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.metadata.CellExtra; import com.alibaba.excel.read.listener.ReadListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Assert; @@ -46,7 +46,7 @@ public class DemoExtraListener implements ReadListener { break; case MERGE: log.info( - "额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", + "额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}", extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(), extra.getLastColumnIndex()); break; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java index ffde2b2c..7e9cf6c6 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/DemoHeadDataListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java index d00d44df..77898d23 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/IndexOrNameDataListener.java +++ b/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.event.AnalysisEventListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java index 13de38d2..ec387ca0 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/NoModelDataListener.java +++ b/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.event.AnalysisEventListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java index 527ca6dc..83ec2c90 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/read/ReadTest.java +++ b/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.metadata.ReadSheet; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Ignore; @@ -47,7 +47,8 @@ public class ReadTest { // since: 3.0.0-beta1 String fileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; // 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭 - // 这里每次会读取100条数据 然后返回过来 直接调用使用数据就行 + // 这里默认每次会读取100条数据 然后返回过来 直接调用使用数据就行 + // 具体需要返回多少行可以在`PageReadListener`的构造函数设置 EasyExcel.read(fileName, DemoData.class, new PageReadListener(dataList -> { for (DemoData demoData : dataList) { log.info("读取到一条数据{}", JSON.toJSONString(demoData)); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java index 54218355..2096cd4d 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/UploadDataListener.java +++ b/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.read.listener.ReadListener; import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java index 0260534f..f1c8e7c2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/web/WebTest.java +++ b/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.util.ListUtils; 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.stereotype.Controller; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index c3c15a3f..705c2b53 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/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 { 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"; try (InputStream inputStream = FileUtils.openInputStream(new File(imagePath))) { List list = ListUtils.newArrayList(); @@ -361,6 +365,7 @@ public class WriteTest { writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex()); // 设置单个单元格多种样式 + // 这里需要设置 inMomery=true 不然会导致无法展示单个单元格多种样式,所以慎用 WriteCellData richTest = new WriteCellData<>(); richTest.setType(CellDataTypeEnum.RICH_TEXT_STRING); writeCellDemoData.setRichText(richTest); @@ -397,6 +402,8 @@ public class WriteTest { String templateFileName = TestFileUtil.getPath() + "demo" + File.separator + "demo.xlsx"; String fileName = TestFileUtil.getPath() + "templateWrite" + System.currentTimeMillis() + ".xlsx"; // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭 + // 这里要注意 withTemplate 的模板文件会全量存储在内存里面,所以尽量不要用于追加文件,如果文件模板文件过大会OOM + // 如果要再文件中追加(无法在一个线程里面处理,可以在一个线程的建议参照多次写入的demo) 建议临时存储到数据库 或者 磁盘缓存(ehcache) 然后再一次性写入 EasyExcel.write(fileName, DemoData.class).withTemplate(templateFileName).sheet().doWrite(data()); } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java index 8a782bbe..283ce133 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Lock2Test.java +++ b/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.WriteFont; import com.alibaba.excel.write.style.HorizontalCellStyleStrategy; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.CollectionUtils; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java index 1a7dffb5..d7003f25 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockDataListener.java +++ b/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.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 模板的读取类 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java index 07cb7989..8e4ec6c9 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/LockTest.java +++ b/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 com.alibaba.excel.EasyExcel; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Ignore; import org.junit.Test; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java index c3a6654a..0f1f0832 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/StyleTest.java +++ b/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 com.alibaba.excel.EasyExcel; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 临时测试 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java index ffb590ee..b7624599 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/Xls03Test.java +++ b/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.util.BeanMapUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.junit.Ignore; import org.junit.Test; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java index adcef54a..22015457 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/cache/CacheTest.java @@ -17,7 +17,7 @@ import org.slf4j.LoggerFactory; import com.alibaba.easyexcel.test.temp.poi.Poi2Test; import com.alibaba.excel.util.FileUtils; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java index e3fdd695..78167563 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvDataListeer.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java index 91293019..2a6c69ce 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/csv/CsvReadTest.java +++ b/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.excel.EasyExcel; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.apache.commons.csv.CSVFormat; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java index 4440f350..280a5046 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/dataformat/DataFormatTest.java +++ b/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.util.TestFileUtil; 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.ss.usermodel.Cell; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java index 57484b74..b9e2c965 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443Test.java +++ b/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.EasyExcel; import com.alibaba.excel.read.listener.PageReadListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Ignore; import org.junit.Test; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java index 500cc85f..25176101 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/LargeDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java index aff72adf..2d532bb2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/large/NoModelLargeDataListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * @author Jiaju Zhuang diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java index 59330bbd..7ce3f587 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/poi/PoiWriteTest.java +++ b/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.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.SXSSFRow; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java index 48512b6c..780b1ce4 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/CommentTest.java +++ b/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.metadata.data.CellData; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 临时测试 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java index 7835c51f..7ee174d9 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HDListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 模板的读取类 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java index c0cd96dd..711e5b9b 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/HeadListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 模板的读取类 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/TestListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/TestListener.java index fb80a9a4..18927169 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/read/TestListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgListener.java index f687416e..161ccda5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgListener.java +++ b/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.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java index fbfde36f..fcb75f11 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/HgTest.java +++ b/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 com.alibaba.excel.EasyExcel; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 测试poi diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java index 46182803..eab1e1ae 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/RepeatListener.java +++ b/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.excel.context.AnalysisContext; import com.alibaba.excel.event.AnalysisEventListener; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; /** * 模板的读取类 diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java similarity index 98% rename from easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java rename to easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Write.java index 77c1285e..d9b59a52 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/simple/Wirte.java +++ b/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.write.metadata.WriteSheet; import com.alibaba.excel.write.metadata.WriteTable; -import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson2.JSON; import lombok.extern.slf4j.Slf4j; import org.junit.Ignore; @@ -28,8 +28,8 @@ import org.slf4j.LoggerFactory; **/ @Ignore @Slf4j -public class Wirte { - private static final Logger LOGGER = LoggerFactory.getLogger(Wirte.class); +public class Write { + private static final Logger LOGGER = LoggerFactory.getLogger(Write.class); @Test public void simpleWrite1() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java index 22126889..a8603f12 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java @@ -1,15 +1,47 @@ 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.List; import java.util.Map; 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.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.FileUtils; import com.alibaba.excel.util.ListUtils; +import com.alibaba.excel.write.metadata.WriteSheet; 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.Test; import org.springframework.cglib.beans.BeanMap; @@ -28,7 +60,6 @@ public class TempWriteTest { .registerConverter(new CustomStringStringConverter()) .doWrite(ListUtils.newArrayList(tempWriteData)); - EasyExcel.write(TestFileUtil.getPath() + "TempWriteTest" + System.currentTimeMillis() + ".xlsx", TempWriteData.class) .sheet() @@ -55,4 +86,97 @@ public class TempWriteTest { 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 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(); + + } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/util/TestUtil.java new file mode 100644 index 00000000..8246f5b9 --- /dev/null +++ b/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); + } + } + +} diff --git a/easyexcel/pom.xml b/easyexcel/pom.xml index 495552c0..2df8f71a 100644 --- a/easyexcel/pom.xml +++ b/easyexcel/pom.xml @@ -11,6 +11,7 @@ ../pom.xml + https://github.com/alibaba/easyexcel jar easyexcel easyexcel diff --git a/img/readme/alipay.png b/img/readme/alipay.png deleted file mode 100644 index 48cbfe08..00000000 Binary files a/img/readme/alipay.png and /dev/null differ diff --git a/img/readme/large.png b/img/readme/large.png index 48223c56..0188af9e 100644 Binary files a/img/readme/large.png and b/img/readme/large.png differ diff --git a/pom.xml b/pom.xml index 70153a8e..cceb0f94 100644 --- a/pom.xml +++ b/pom.xml @@ -20,7 +20,7 @@ - 3.2.0 + 3.2.1 UTF-8 1.8 true @@ -143,9 +143,9 @@ 1.2.5 - com.alibaba - fastjson - 2.0.6 + com.alibaba.fastjson2 + fastjson2 + 2.0.23 org.springframework.boot diff --git a/update.md b/update.md index 36c98000..f315c5b3 100644 --- a/update.md +++ b/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 * 修复部分xlsx读取日期可能相差1秒的bug [Issue #1956](https://github.com/alibaba/easyexcel/issues/1956) @@ -10,7 +17,8 @@ # 3.1.4 -* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列 +* 提高xlsx读取兼容性:在存在第一行很多空 +* 列的情况下,忽略空列 # 3.1.3