Browse Source

* 读写支持`try-with-resources`语法糖

pull/2472/head
Jiaju Zhuang 3 years ago
parent
commit
b67f7d03d0
  1. 6
      easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java
  2. 6
      easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java
  3. 6
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  4. 2
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java
  5. 2
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java
  6. 8
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java
  7. 2
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java
  8. 2
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java
  9. 2
      easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java
  10. 2
      easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java
  11. 4
      easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java
  12. 2
      easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java
  13. 9
      easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  14. 2
      easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java
  15. 3
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java
  16. 2
      easyexcel-core/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java
  17. 1
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java
  18. 2
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java
  19. 2
      easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java
  20. 6
      easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java
  21. 8
      easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java
  22. 2
      easyexcel-core/src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java
  23. 4
      easyexcel-core/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java
  24. 9
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/charset/CharsetDataTest.java
  25. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java
  26. 11
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java
  27. 5
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java
  28. 8
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java
  29. 5
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java
  30. 22
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java
  31. 10
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java
  32. 6
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java
  33. 1
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java
  34. 2
      easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443.java

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

@ -1,7 +1,6 @@
package com.alibaba.excel; package com.alibaba.excel;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -12,9 +11,6 @@ import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.ReadWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Excel readers are all read in event mode. * Excel readers are all read in event mode.
* *
@ -25,7 +21,7 @@ public class ExcelReader implements Closeable {
/** /**
* Analyser * Analyser
*/ */
private ExcelAnalyser excelAnalyser; private final ExcelAnalyser excelAnalyser;
public ExcelReader(ReadWorkbook readWorkbook) { public ExcelReader(ReadWorkbook readWorkbook) {
excelAnalyser = new ExcelAnalyserImpl(readWorkbook); excelAnalyser = new ExcelAnalyserImpl(readWorkbook);

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

@ -1,7 +1,6 @@
package com.alibaba.excel; package com.alibaba.excel;
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -13,9 +12,6 @@ import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.WriteWorkbook;
import com.alibaba.excel.write.metadata.fill.FillConfig; import com.alibaba.excel.write.metadata.fill.FillConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two * Excel Writer This tool is used to write value out to Excel via POI. This object can perform the following two
* functions. * functions.
@ -29,7 +25,7 @@ import org.slf4j.LoggerFactory;
*/ */
public class ExcelWriter implements Closeable { public class ExcelWriter implements Closeable {
private ExcelBuilder excelBuilder; private final ExcelBuilder excelBuilder;
/** /**
* Create new writer * Create new writer

6
easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java

@ -1,7 +1,5 @@
package com.alibaba.excel.analysis.csv; package com.alibaba.excel.analysis.csv;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.file.Files; import java.nio.file.Files;
@ -38,8 +36,8 @@ import org.apache.commons.csv.CSVRecord;
@Slf4j @Slf4j
public class CsvExcelReadExecutor implements ExcelReadExecutor { public class CsvExcelReadExecutor implements ExcelReadExecutor {
private List<ReadSheet> sheetList; private final List<ReadSheet> sheetList;
private CsvReadContext csvReadContext; private final CsvReadContext csvReadContext;
public CsvExcelReadExecutor(CsvReadContext csvReadContext) { public CsvExcelReadExecutor(CsvReadContext csvReadContext) {
this.csvReadContext = csvReadContext; this.csvReadContext = csvReadContext;

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

@ -25,7 +25,7 @@ import com.alibaba.excel.exception.ExcelAnalysisException;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class XlsListSheetListener implements HSSFListener { public class XlsListSheetListener implements HSSFListener {
private XlsReadContext xlsReadContext; private final XlsReadContext xlsReadContext;
private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(); private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>();
static { static {

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

@ -77,7 +77,7 @@ public class XlsSaxAnalyser implements HSSFListener, ExcelReadExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class); private static final Logger LOGGER = LoggerFactory.getLogger(XlsSaxAnalyser.class);
private static final short DUMMY_RECORD_SID = -1; private static final short DUMMY_RECORD_SID = -1;
private XlsReadContext xlsReadContext; private final XlsReadContext xlsReadContext;
private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32); private static final Map<Short, XlsRecordHandler> XLS_RECORD_HANDLER_MAP = new HashMap<Short, XlsRecordHandler>(32);
static { static {

8
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java

@ -52,13 +52,13 @@ import org.xml.sax.XMLReader;
@Slf4j @Slf4j
public class XlsxSaxAnalyser implements ExcelReadExecutor { public class XlsxSaxAnalyser implements ExcelReadExecutor {
private XlsxReadContext xlsxReadContext; private final XlsxReadContext xlsxReadContext;
private List<ReadSheet> sheetList; private final List<ReadSheet> sheetList;
private Map<Integer, InputStream> sheetMap; private final Map<Integer, InputStream> sheetMap;
/** /**
* excel comments key: sheetNo value: CommentsTable * excel comments key: sheetNo value: CommentsTable
*/ */
private Map<Integer, CommentsTable> commentsTableMap; private final Map<Integer, CommentsTable> commentsTableMap;
public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception { public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception {
this.xlsxReadContext = xlsxReadContext; this.xlsxReadContext = xlsxReadContext;

2
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java

@ -27,7 +27,7 @@ public class SharedStringsTableHandler extends DefaultHandler {
*/ */
private StringBuilder currentElementData; private StringBuilder currentElementData;
private ReadCache readCache; private final ReadCache readCache;
/** /**
* Some fields in the T tag need to be ignored * Some fields in the T tag need to be ignored
*/ */

2
easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java

@ -25,7 +25,7 @@ import org.xml.sax.helpers.DefaultHandler;
*/ */
@Slf4j @Slf4j
public class XlsxRowHandler extends DefaultHandler { public class XlsxRowHandler extends DefaultHandler {
private XlsxReadContext xlsxReadContext; private final XlsxReadContext xlsxReadContext;
private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32); private static final Map<String, XlsxTagHandler> XLSX_CELL_HANDLER_MAP = new HashMap<String, XlsxTagHandler>(32);
static { static {

2
easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java vendored

@ -11,7 +11,7 @@ import com.alibaba.excel.context.AnalysisContext;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class MapCache implements ReadCache { public class MapCache implements ReadCache {
private List<String> cache = new ArrayList<>(); private final List<String> cache = new ArrayList<>();
@Override @Override
public void init(AnalysisContext analysisContext) {} public void init(AnalysisContext analysisContext) {}

2
easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java vendored

@ -11,7 +11,7 @@ import com.alibaba.excel.context.AnalysisContext;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class XlsCache implements ReadCache { public class XlsCache implements ReadCache {
private SSTRecord sstRecord; private final SSTRecord sstRecord;
public XlsCache(SSTRecord sstRecord) { public XlsCache(SSTRecord sstRecord) {
this.sstRecord = sstRecord; this.sstRecord = sstRecord;

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

@ -33,12 +33,12 @@ public class SimpleReadCacheSelector implements ReadCacheSelector {
/** /**
* Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB. * Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB.
*/ */
private long maxUseMapCacheSize; private final long maxUseMapCacheSize;
/** /**
* Maximum size of cache activation.unit MB. * Maximum size of cache activation.unit MB.
*/ */
private int maxCacheActivateSize; private final int maxCacheActivateSize;
public SimpleReadCacheSelector() { public SimpleReadCacheSelector() {
this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE); this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE);

2
easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java

@ -46,7 +46,7 @@ public class AnalysisContextImpl implements AnalysisContext {
/** /**
* Event processor * Event processor
*/ */
private AnalysisEventProcessor analysisEventProcessor; private final AnalysisEventProcessor analysisEventProcessor;
public AnalysisContextImpl(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) { public AnalysisContextImpl(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) {
if (readWorkbook == null) { if (readWorkbook == null) {

9
easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java

@ -28,7 +28,7 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
/** /**
* Workbook * Workbook
*/ */
private ReadWorkbook readWorkbook; private final ReadWorkbook readWorkbook;
public ExcelReaderBuilder() { public ExcelReaderBuilder() {
this.readWorkbook = new ReadWorkbook(); this.readWorkbook = new ReadWorkbook();
@ -203,9 +203,9 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
} }
public void doReadAll() { public void doReadAll() {
ExcelReader excelReader = build(); try (ExcelReader excelReader = build()) {
excelReader.readAll(); excelReader.readAll();
excelReader.finish(); }
} }
/** /**
@ -216,9 +216,10 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
public <T> List<T> doReadAllSync() { public <T> List<T> doReadAllSync() {
SyncReadListener syncReadListener = new SyncReadListener(); SyncReadListener syncReadListener = new SyncReadListener();
registerReadListener(syncReadListener); registerReadListener(syncReadListener);
ExcelReader excelReader = build(); try (ExcelReader excelReader = build()) {
excelReader.readAll(); excelReader.readAll();
excelReader.finish(); excelReader.finish();
}
return (List<T>)syncReadListener.getList(); return (List<T>)syncReadListener.getList();
} }

2
easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java

@ -18,7 +18,7 @@ public class ExcelReaderSheetBuilder extends AbstractExcelReaderParameterBuilder
/** /**
* Sheet * Sheet
*/ */
private ReadSheet readSheet; private final ReadSheet readSheet;
public ExcelReaderSheetBuilder() { public ExcelReaderSheetBuilder() {
this.readSheet = new ReadSheet(); this.readSheet = new ReadSheet();

3
easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java

@ -4,12 +4,9 @@ import com.alibaba.excel.read.metadata.ReadWorkbook;
import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.StringUtils;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.compress.utils.CharsetNames;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVParser;

2
easyexcel-core/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java

@ -22,7 +22,7 @@ import org.apache.poi.ss.util.CellRangeAddress;
*/ */
public class ExcelBuilderImpl implements ExcelBuilder { public class ExcelBuilderImpl implements ExcelBuilder {
private WriteContext context; private final WriteContext context;
private ExcelWriteFillExecutor excelWriteFillExecutor; private ExcelWriteFillExecutor excelWriteFillExecutor;
private ExcelWriteAddExecutor excelWriteAddExecutor; private ExcelWriteAddExecutor excelWriteAddExecutor;

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

@ -6,7 +6,6 @@ import java.io.OutputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import com.alibaba.excel.ExcelWriter; import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteWorkbook; import com.alibaba.excel.write.metadata.WriteWorkbook;

2
easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java

@ -18,7 +18,7 @@ public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder
/** /**
* Sheet * Sheet
*/ */
private WriteSheet writeSheet; private final WriteSheet writeSheet;
public ExcelWriterSheetBuilder() { public ExcelWriterSheetBuilder() {
this.writeSheet = new WriteSheet(); this.writeSheet = new WriteSheet();

2
easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java

@ -21,7 +21,7 @@ public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder
/** /**
* table * table
*/ */
private WriteTable writeTable; private final WriteTable writeTable;
public ExcelWriterTableBuilder() { public ExcelWriterTableBuilder() {
this.writeTable = new WriteTable(); this.writeTable = new WriteTable();

6
easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java

@ -15,15 +15,15 @@ public class LoopMergeStrategy implements RowWriteHandler {
/** /**
* Each row * Each row
*/ */
private int eachRow; private final int eachRow;
/** /**
* Extend column * Extend column
*/ */
private int columnExtend; private final int columnExtend;
/** /**
* The number of the current column * The number of the current column
*/ */
private int columnIndex; private final int columnIndex;
public LoopMergeStrategy(int eachRow, int columnIndex) { public LoopMergeStrategy(int eachRow, int columnIndex) {
this(eachRow, 1, columnIndex); this(eachRow, 1, columnIndex);

8
easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java

@ -16,19 +16,19 @@ public class OnceAbsoluteMergeStrategy implements SheetWriteHandler {
/** /**
* First row * First row
*/ */
private int firstRowIndex; private final int firstRowIndex;
/** /**
* Last row * Last row
*/ */
private int lastRowIndex; private final int lastRowIndex;
/** /**
* First column * First column
*/ */
private int firstColumnIndex; private final int firstColumnIndex;
/** /**
* Last row * Last row
*/ */
private int lastColumnIndex; private final int lastColumnIndex;
public OnceAbsoluteMergeStrategy(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex) { public OnceAbsoluteMergeStrategy(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex) {
if (firstRowIndex < 0 || lastRowIndex < 0 || firstColumnIndex < 0 || lastColumnIndex < 0) { if (firstRowIndex < 0 || lastRowIndex < 0 || firstColumnIndex < 0 || lastColumnIndex < 0) {

2
easyexcel-core/src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java

@ -8,7 +8,7 @@ import com.alibaba.excel.metadata.Head;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy { public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy {
private Integer columnWidth; private final Integer columnWidth;
/** /**
* *

4
easyexcel-core/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java

@ -8,8 +8,8 @@ import org.apache.poi.ss.usermodel.Row;
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy { public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy {
private Short headRowHeight; private final Short headRowHeight;
private Short contentRowHeight; private final Short contentRowHeight;
public SimpleRowHeightStyleStrategy(Short headRowHeight, Short contentRowHeight) { public SimpleRowHeightStyleStrategy(Short headRowHeight, Short contentRowHeight) {
this.headRowHeight = headRowHeight; this.headRowHeight = headRowHeight;

9
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/charset/CharsetDataTest.java

@ -3,25 +3,16 @@ package com.alibaba.easyexcel.test.core.charset;
import java.io.File; import java.io.File;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.alibaba.easyexcel.test.core.simple.SimpleData;
import com.alibaba.easyexcel.test.core.simple.SimpleDataListener;
import com.alibaba.easyexcel.test.demo.read.DemoData;
import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.easyexcel.test.util.TestFileUtil;
import com.alibaba.excel.EasyExcel; import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext; import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.data.ReadCellData; import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.excel.read.listener.ReadListener; import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ConverterUtils;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Charsets;
import org.apache.commons.compress.utils.Lists; import org.apache.commons.compress.utils.Lists;
import org.junit.Assert; import org.junit.Assert;
import org.junit.BeforeClass; import org.junit.BeforeClass;

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

@ -21,7 +21,7 @@ import org.slf4j.LoggerFactory;
*/ */
public class ConverterDataListener extends AnalysisEventListener<ConverterReadData> { public class ConverterDataListener extends AnalysisEventListener<ConverterReadData> {
private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class);
private List<ConverterReadData> list = new ArrayList<>(); private final List<ConverterReadData> list = new ArrayList<>();
@Override @Override
public void invoke(ConverterReadData data, AnalysisContext context) { public void invoke(ConverterReadData data, AnalysisContext context) {

11
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java

@ -142,7 +142,7 @@ public class FillDataTest {
} }
private void compositeFill(File file, File template) { private void compositeFill(File file, File template) {
ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
@ -155,7 +155,7 @@ public class FillDataTest {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
map.put("date", "2019年10月9日13:28:28"); map.put("date", "2019年10月9日13:28:28");
excelWriter.fill(map, writeSheet); excelWriter.fill(map, writeSheet);
excelWriter.finish(); }
List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync();
Map<String, String> map0 = (Map<String, String>)list.get(0); Map<String, String> map0 = (Map<String, String>)list.get(0);
@ -167,7 +167,7 @@ public class FillDataTest {
} }
private void horizontalFill(File file, File template) { private void horizontalFill(File file, File template) {
ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
excelWriter.fill(data(), fillConfig, writeSheet); excelWriter.fill(data(), fillConfig, writeSheet);
@ -176,6 +176,7 @@ public class FillDataTest {
map.put("date", "2019年10月9日13:28:28"); map.put("date", "2019年10月9日13:28:28");
excelWriter.fill(map, writeSheet); excelWriter.fill(map, writeSheet);
excelWriter.finish(); excelWriter.finish();
}
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync();
Assert.assertEquals(list.size(), 5L); Assert.assertEquals(list.size(), 5L);
@ -184,7 +185,7 @@ public class FillDataTest {
} }
private void complexFill(File file, File template) { private void complexFill(File file, File template) {
ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build(); WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build();
FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
excelWriter.fill(data(), fillConfig, writeSheet); excelWriter.fill(data(), fillConfig, writeSheet);
@ -193,7 +194,7 @@ public class FillDataTest {
map.put("date", "2019年10月9日13:28:28"); map.put("date", "2019年10月9日13:28:28");
map.put("total", 1000); map.put("total", 1000);
excelWriter.fill(map, writeSheet); excelWriter.fill(map, writeSheet);
excelWriter.finish(); }
List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); List<Object> list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync();
Assert.assertEquals(list.size(), 21L); Assert.assertEquals(list.size(), 21L);
Map<String, String> map19 = (Map<String, String>)list.get(19); Map<String, String> map19 = (Map<String, String>)list.get(19);

5
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java

@ -61,7 +61,7 @@ public class FillAnnotationDataTest {
private void readAndWrite(File file, File fileTemplate) throws Exception { private void readAndWrite(File file, File fileTemplate) throws Exception {
EasyExcel.write().file(file).head(FillAnnotationData.class).withTemplate(fileTemplate).sheet().doFill(data()); EasyExcel.write().file(file).head(FillAnnotationData.class).withTemplate(fileTemplate).sheet().doFill(data());
Workbook workbook = WorkbookFactory.create(file); try (Workbook workbook = WorkbookFactory.create(file)) {
Sheet sheet = workbook.getSheetAt(0); Sheet sheet = workbook.getSheetAt(0);
Row row1 = sheet.getRow(1); Row row1 = sheet.getRow(1);
@ -78,6 +78,7 @@ public class FillAnnotationDataTest {
if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1 if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1
&& mergedRegion.getFirstColumn() == 2 && mergedRegion.getLastColumn() == 3) { && mergedRegion.getFirstColumn() == 2 && mergedRegion.getLastColumn() == 3) {
hasMerge = true; hasMerge = true;
break;
} }
} }
Assert.assertTrue(hasMerge); Assert.assertTrue(hasMerge);
@ -100,7 +101,7 @@ public class FillAnnotationDataTest {
Assert.assertEquals(1, anchor.getRow1()); Assert.assertEquals(1, anchor.getRow1());
Assert.assertEquals(4, anchor.getCol1()); Assert.assertEquals(4, anchor.getCol1());
} }
}
} }
private List<FillAnnotationData> data() throws Exception { private List<FillAnnotationData> data() throws Exception {

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

@ -57,26 +57,26 @@ public class LargeDataTest {
@Test @Test
public void t02Fill() { public void t02Fill() {
ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build(); try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) { for (int j = 0; j < 5000; j++) {
excelWriter.fill(data(), writeSheet); excelWriter.fill(data(), writeSheet);
LOGGER.info("{} fill success.", j); LOGGER.info("{} fill success.", j);
} }
excelWriter.finish(); }
} }
@Test @Test
public void t03ReadAndWriteCsv() { public void t03ReadAndWriteCsv() {
// write // write
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
ExcelWriter excelWriter = EasyExcel.write(fileCsv).build(); try (ExcelWriter excelWriter = EasyExcel.write(fileCsv).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build(); WriteSheet writeSheet = EasyExcel.writerSheet().build();
for (int j = 0; j < 5000; j++) { for (int j = 0; j < 5000; j++) {
excelWriter.write(data(), writeSheet); excelWriter.write(data(), writeSheet);
LOGGER.info("{} write success.", j); LOGGER.info("{} write success.", j);
} }
excelWriter.finish(); }
LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start);
// read // read

5
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java

@ -15,7 +15,6 @@ import org.junit.Test;
import org.junit.runners.MethodSorters; import org.junit.runners.MethodSorters;
/** /**
*
* @author Jiaju Zhuang * @author Jiaju Zhuang
*/ */
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
@ -52,7 +51,7 @@ public class MultipleSheetsDataTest {
private void read(File file) { private void read(File file) {
MultipleSheetsListener multipleSheetsListener = new MultipleSheetsListener(); MultipleSheetsListener multipleSheetsListener = new MultipleSheetsListener();
ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build(); try (ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build()) {
List<ReadSheet> sheets = excelReader.excelExecutor().sheetList(); List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
int count = 1; int count = 1;
for (ReadSheet readSheet : sheets) { for (ReadSheet readSheet : sheets) {
@ -60,7 +59,7 @@ public class MultipleSheetsDataTest {
Assert.assertEquals(multipleSheetsListener.getList().size(), count); Assert.assertEquals(multipleSheetsListener.getList().size(), count);
count++; count++;
} }
excelReader.finish(); }
} }
private void readAll(File file) { private void readAll(File file) {

22
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java

@ -56,12 +56,15 @@ public class RepetitionDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build(); try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
excelWriter.write(data(), writeSheet).write(data(), writeSheet).finish(); excelWriter.write(data(), writeSheet).write(data(), writeSheet);
ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build(); }
try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener())
.build()) {
ReadSheet readSheet = EasyExcel.readSheet(0).build(); ReadSheet readSheet = EasyExcel.readSheet(0).build();
excelReader.read(readSheet).finish(); excelReader.read(readSheet);
}
} }
@Test @Test
@ -80,13 +83,16 @@ public class RepetitionDataTest {
} }
private void readAndWriteTable(File file) { private void readAndWriteTable(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build(); try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); WriteSheet writeSheet = EasyExcel.writerSheet(0).build();
WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build(); WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build();
excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable).finish(); excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable);
ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build(); }
try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener())
.build()) {
ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build(); ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build();
excelReader.read(readSheet).finish(); excelReader.read(readSheet);
}
} }
private List<RepetitionData> data() { private List<RepetitionData> data() {

10
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java

@ -53,7 +53,7 @@ public class SkipDataTest {
} }
private void readAndWrite(File file) { private void readAndWrite(File file) {
ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build(); try (ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build();) {
WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build(); WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build();
WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build(); WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build();
WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build(); WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build();
@ -62,15 +62,15 @@ public class SkipDataTest {
excelWriter.write(data("name2"), writeSheet1); excelWriter.write(data("name2"), writeSheet1);
excelWriter.write(data("name3"), writeSheet2); excelWriter.write(data("name3"), writeSheet2);
excelWriter.write(data("name4"), writeSheet3); excelWriter.write(data("name4"), writeSheet3);
excelWriter.finish(); }
List<SkipData> list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync(); List<SkipData> list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync();
Assert.assertEquals(1, list.size()); Assert.assertEquals(1, list.size());
Assert.assertEquals("name2", list.get(0).getName()); Assert.assertEquals("name2", list.get(0).getName());
SyncReadListener syncReadListener = new SyncReadListener(); SyncReadListener syncReadListener = new SyncReadListener();
ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener) try (ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener)
.build(); .build()) {
ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build(); ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build();
ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build(); ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build();
excelReader.read(readSheet1, readSheet3); excelReader.read(readSheet1, readSheet3);
@ -78,7 +78,7 @@ public class SkipDataTest {
Assert.assertEquals(2, syncList.size()); Assert.assertEquals(2, syncList.size());
Assert.assertEquals("name2", ((SkipData)syncList.get(0)).getName()); Assert.assertEquals("name2", ((SkipData)syncList.get(0)).getName());
Assert.assertEquals("name4", ((SkipData)syncList.get(1)).getName()); Assert.assertEquals("name4", ((SkipData)syncList.get(1)).getName());
excelReader.finish(); }
} }
private List<SkipData> data(String name) { private List<SkipData> data(String name) {

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

@ -216,7 +216,7 @@ public class WriteTest {
} finally { } finally {
// 千万别忘记close 会帮忙关闭流 // 千万别忘记close 会帮忙关闭流
if (writer != null) { if (writer != null) {
writer.finish(); writer.close();
} }
} }
@ -250,7 +250,7 @@ public class WriteTest {
} finally { } finally {
// 千万别忘记close 会帮忙关闭流 // 千万别忘记close 会帮忙关闭流
if (writer != null) { if (writer != null) {
writer.finish(); writer.close();
} }
} }
@ -286,7 +286,7 @@ public class WriteTest {
} finally { } finally {
// 千万别忘记close 会帮忙关闭流 // 千万别忘记close 会帮忙关闭流
if (writer != null) { if (writer != null) {
writer.finish(); writer.close();
} }
} }
} }

1
easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java

@ -28,7 +28,6 @@ import org.slf4j.LoggerFactory;
public class WriteLargeTest { public class WriteLargeTest {
private static final Logger LOGGER = LoggerFactory.getLogger(WriteLargeTest.class); private static final Logger LOGGER = LoggerFactory.getLogger(WriteLargeTest.class);
private int i = 0;
@Test @Test
public void test() throws Exception { public void test() throws Exception {

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

@ -4,8 +4,6 @@ import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.Date;
@Getter @Getter
@Setter @Setter
@EqualsAndHashCode @EqualsAndHashCode

Loading…
Cancel
Save