commit
5fa867b1b8
2
.github/ISSUE_TEMPLATE/bug.md
vendored
2
.github/ISSUE_TEMPLATE/bug.md
vendored
@ -16,3 +16,5 @@ assignees: zhuangjiaju
|
||||
这里写代码
|
||||
```
|
||||
# 提示的异常或者没有达到的效果
|
||||
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
|
||||
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。
|
||||
|
1
.github/ISSUE_TEMPLATE/question.md
vendored
1
.github/ISSUE_TEMPLATE/question.md
vendored
@ -15,4 +15,5 @@ assignees: ''
|
||||
```
|
||||
# 异常提示
|
||||
大家尽量把问题一次性描述清楚,然后贴上全部异常,这样方便把问题一次性解决掉。
|
||||
至少大家要符合一个原则就是,能让其他人复现出这个问题,如果无法复现,肯定无法解决。
|
||||
# 问题描述
|
||||
|
2
.github/workflows/sync2gitee.yml
vendored
2
.github/workflows/sync2gitee.yml
vendored
@ -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:
|
||||
|
@ -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解析
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<version>3.2.0</version>
|
||||
<version>3.2.1</version>
|
||||
</dependency>
|
||||
```
|
||||
|
||||
|
@ -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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>easyexcel-parent</artifactId>
|
||||
@ -11,6 +10,7 @@
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<url>https://github.com/alibaba/easyexcel</url>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>easyexcel-core</artifactId>
|
||||
<name>easyexcel-core</name>
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Integer, ArrayList> FILE_CACHE_CONFIGURATION;
|
||||
private static final CacheManager ACTIVE_CACHE_MANAGER;
|
||||
private static final File CACHE_PATH_FILE;
|
||||
|
||||
private final CacheConfiguration<Integer, ArrayList> 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);
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
*/
|
||||
private final int maxCacheActivateSize;
|
||||
@Deprecated
|
||||
private Integer maxCacheActivateSize;
|
||||
|
||||
/**
|
||||
* Maximum batch of `SharedStrings` stored in memory.
|
||||
* The batch size is 100.{@link Ehcache#BATCH_COUNT}
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.alibaba.excel.converters.localdatetime;
|
||||
package com.alibaba.excel.converters.localdate;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import com.alibaba.excel.converters.Converter;
|
||||
@ -14,15 +15,15 @@ import com.alibaba.excel.util.DateUtils;
|
||||
import org.apache.poi.ss.usermodel.DateUtil;
|
||||
|
||||
/**
|
||||
* LocalDateTime and number converter
|
||||
* LocalDate and number converter
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
public class LocalDateNumberConverter implements Converter<LocalDateTime> {
|
||||
public class LocalDateNumberConverter implements Converter<LocalDate> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return LocalDateTime.class;
|
||||
return LocalDate.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -31,19 +32,19 @@ public class LocalDateNumberConverter implements Converter<LocalDateTime> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
||||
public LocalDate convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
||||
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
|
||||
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
|
||||
globalConfiguration.getUse1904windowing());
|
||||
} else {
|
||||
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
|
||||
return DateUtils.getLocalDate(cellData.getNumberValue().doubleValue(),
|
||||
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(LocalDateTime value, ExcelContentProperty contentProperty,
|
||||
public WriteCellData<?> convertToExcelData(LocalDate value, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
||||
return new WriteCellData<>(
|
@ -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()));
|
||||
}
|
||||
}
|
||||
}
|
@ -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<LocalDateTime> {
|
||||
@Override
|
||||
public Class<LocalDateTime> supportJavaTypeKey() {
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return LocalDateTime.class;
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,56 @@
|
||||
package com.alibaba.excel.converters.localdatetime;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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;
|
||||
|
||||
/**
|
||||
* LocalDateTime and number converter
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
public class LocalDateTimeNumberConverter implements Converter<LocalDateTime> {
|
||||
|
||||
@Override
|
||||
public Class<?> supportJavaTypeKey() {
|
||||
return LocalDateTime.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CellDataTypeEnum supportExcelTypeKey() {
|
||||
return CellDataTypeEnum.NUMBER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LocalDateTime convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
|
||||
GlobalConfiguration globalConfiguration) {
|
||||
if (contentProperty == null || contentProperty.getDateTimeFormatProperty() == null) {
|
||||
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
|
||||
globalConfiguration.getUse1904windowing());
|
||||
} else {
|
||||
return DateUtils.getLocalDateTime(cellData.getNumberValue().doubleValue(),
|
||||
contentProperty.getDateTimeFormatProperty().getUse1904windowing());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WriteCellData<?> convertToExcelData(LocalDateTime 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())));
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ import lombok.Setter;
|
||||
import org.apache.poi.ss.usermodel.CellStyle;
|
||||
|
||||
/**
|
||||
* wirte cell data
|
||||
* write cell data
|
||||
*
|
||||
* @author Jiaju Zhuang
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@ import org.apache.commons.collections4.CollectionUtils;
|
||||
*/
|
||||
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;
|
||||
/**
|
||||
@ -27,16 +27,26 @@ public class PageReadListener<T> implements ReadListener<T> {
|
||||
*/
|
||||
private final Consumer<List<T>> consumer;
|
||||
|
||||
/**
|
||||
* Single handle the amount of data
|
||||
*/
|
||||
private final int batchCount;
|
||||
|
||||
public PageReadListener(Consumer<List<T>> consumer) {
|
||||
this(consumer, BATCH_COUNT);
|
||||
}
|
||||
|
||||
public PageReadListener(Consumer<List<T>> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -66,7 +66,6 @@ public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder<Exce
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public ExcelWriterBuilder excelType(ExcelTypeEnum excelType) {
|
||||
writeWorkbook.setExcelType(excelType);
|
||||
return this;
|
||||
@ -95,21 +94,32 @@ public class ExcelWriterBuilder extends AbstractExcelWriterParameterBuilder<Exce
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template file.
|
||||
* This file is read into memory, excessive cases can lead to OOM.
|
||||
*/
|
||||
public ExcelWriterBuilder withTemplate(InputStream templateInputStream) {
|
||||
writeWorkbook.setTemplateInputStream(templateInputStream);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template file.
|
||||
* This file is read into memory, excessive cases can lead to OOM.
|
||||
*/
|
||||
public ExcelWriterBuilder withTemplate(File templateFile) {
|
||||
writeWorkbook.setTemplateFile(templateFile);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Template file.
|
||||
* This file is read into memory, excessive cases can lead to OOM.
|
||||
*/
|
||||
public ExcelWriterBuilder withTemplate(String pathName) {
|
||||
return withTemplate(new File(pathName));
|
||||
}
|
||||
|
||||
|
||||
public ExcelWriter build() {
|
||||
return new ExcelWriter(writeWorkbook);
|
||||
}
|
||||
|
@ -49,7 +49,8 @@ public class WriteWorkbook extends WriteBasicParameter {
|
||||
private InputStream templateInputStream;
|
||||
|
||||
/**
|
||||
* Template file
|
||||
* Template file.
|
||||
* This file is read into memory, excessive cases can lead to OOM.
|
||||
* <p>
|
||||
* If 'inputStream' and 'file' all not empty, file first
|
||||
*/
|
||||
@ -66,7 +67,6 @@ public class WriteWorkbook extends WriteBasicParameter {
|
||||
* Whether the encryption
|
||||
* <p>
|
||||
* WARRING:Encryption is when the entire file is read into memory, so it is very memory intensive.
|
||||
*
|
||||
*/
|
||||
private String password;
|
||||
/**
|
||||
|
@ -11,6 +11,7 @@
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<url>https://github.com/alibaba/easyexcel</url>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>easyexcel-support</artifactId>
|
||||
<name>easyexcel-support</name>
|
||||
|
@ -11,6 +11,7 @@
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<url>https://github.com/alibaba/easyexcel</url>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>easyexcel-test</artifactId>
|
||||
<name>easyexcel-test</name>
|
||||
@ -31,8 +32,8 @@
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
@ -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<ConverterReadDa
|
||||
public void doAfterAllAnalysed(AnalysisContext context) {
|
||||
Assert.assertEquals(list.size(), 1);
|
||||
ConverterReadData data = list.get(0);
|
||||
try {
|
||||
Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01"), data.getDate());
|
||||
} catch (ParseException e) {
|
||||
throw new ExcelCommonException("Test Exception", e);
|
||||
}
|
||||
Assert.assertEquals(DateUtils.parseLocalDateTime("2020-01-01 01:01:01", null, null), data.getLocalDateTime());
|
||||
Assert.assertEquals(TestUtil.TEST_DATE, data.getDate());
|
||||
Assert.assertEquals(TestUtil.TEST_LOCAL_DATE, data.getLocalDate());
|
||||
Assert.assertEquals(TestUtil.TEST_LOCAL_DATE_TIME, data.getLocalDateTime());
|
||||
Assert.assertEquals(data.getBooleanData(), Boolean.TRUE);
|
||||
Assert.assertEquals(data.getBigDecimal().doubleValue(), BigDecimal.ONE.doubleValue(), 0.0);
|
||||
Assert.assertEquals(data.getBigInteger().intValue(), BigInteger.ONE.intValue(), 0.0);
|
||||
|
@ -4,10 +4,13 @@ import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.easyexcel.test.util.TestFileUtil;
|
||||
import com.alibaba.easyexcel.test.util.TestUtil;
|
||||
import com.alibaba.excel.EasyExcel;
|
||||
import com.alibaba.excel.metadata.data.WriteCellData;
|
||||
import com.alibaba.excel.util.DateUtils;
|
||||
@ -112,8 +115,9 @@ public class ConverterDataTest {
|
||||
private List<ConverterWriteData> data() throws Exception {
|
||||
List<ConverterWriteData> list = new ArrayList<ConverterWriteData>();
|
||||
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);
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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()) {
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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<Map<Integer, Object>> 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<SimpleData>(dataList -> {
|
||||
Assert.assertEquals(5, dataList.size());
|
||||
}, 5))
|
||||
.sheet().doRead();
|
||||
}
|
||||
|
||||
private void synchronousRead(File file) {
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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";
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<DemoExtraData> {
|
||||
break;
|
||||
case MERGE:
|
||||
log.info(
|
||||
"额外信息是超链接,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}",
|
||||
"额外信息是合并单元格,而且覆盖了一个区间,在firstRowIndex:{},firstColumnIndex;{},lastRowIndex:{},lastColumnIndex:{}",
|
||||
extra.getFirstRowIndex(), extra.getFirstColumnIndex(), extra.getLastRowIndex(),
|
||||
extra.getLastColumnIndex());
|
||||
break;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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<DemoData>(dataList -> {
|
||||
for (DemoData demoData : dataList) {
|
||||
log.info("读取到一条数据{}", JSON.toJSONString(demoData));
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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<ImageDemoData> list = ListUtils.newArrayList();
|
||||
@ -361,6 +365,7 @@ public class WriteTest {
|
||||
writeCellStyleData.setFillForegroundColor(IndexedColors.GREEN.getIndex());
|
||||
|
||||
// 设置单个单元格多种样式
|
||||
// 这里需要设置 inMomery=true 不然会导致无法展示单个单元格多种样式,所以慎用
|
||||
WriteCellData<String> 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());
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 模板的读取类
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 临时测试
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 临时测试
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 模板的读取类
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 模板的读取类
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
/**
|
||||
* 模板的读取类
|
||||
|
@ -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() {
|
@ -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<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();
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -11,6 +11,7 @@
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<url>https://github.com/alibaba/easyexcel</url>
|
||||
<packaging>jar</packaging>
|
||||
<artifactId>easyexcel</artifactId>
|
||||
<name>easyexcel</name>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 22 KiB |
Binary file not shown.
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 20 KiB |
8
pom.xml
8
pom.xml
@ -20,7 +20,7 @@
|
||||
|
||||
|
||||
<properties>
|
||||
<revision>3.2.0</revision>
|
||||
<revision>3.2.1</revision>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<jdk.version>1.8</jdk.version>
|
||||
<gpg.skip>true</gpg.skip>
|
||||
@ -143,9 +143,9 @@
|
||||
<version>1.2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>2.0.6</version>
|
||||
<groupId>com.alibaba.fastjson2</groupId>
|
||||
<artifactId>fastjson2</artifactId>
|
||||
<version>2.0.23</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
10
update.md
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
|
||||
|
||||
* 修复部分xlsx读取日期可能相差1秒的bug [Issue #1956](https://github.com/alibaba/easyexcel/issues/1956)
|
||||
@ -10,7 +17,8 @@
|
||||
|
||||
# 3.1.4
|
||||
|
||||
* 提高xlsx读取兼容性:在存在第一行很多空列的情况下,忽略空列
|
||||
* 提高xlsx读取兼容性:在存在第一行很多空
|
||||
* 列的情况下,忽略空列
|
||||
|
||||
# 3.1.3
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user