diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java index ab3f64a5..9508d941 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelReader.java @@ -1,7 +1,6 @@ package com.alibaba.excel; import java.io.Closeable; -import java.io.IOException; import java.util.Arrays; 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.ReadWorkbook; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * Excel readers are all read in event mode. * @@ -25,7 +21,7 @@ public class ExcelReader implements Closeable { /** * Analyser */ - private ExcelAnalyser excelAnalyser; + private final ExcelAnalyser excelAnalyser; public ExcelReader(ReadWorkbook readWorkbook) { excelAnalyser = new ExcelAnalyserImpl(readWorkbook); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java index 13e38d63..95d5446b 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/ExcelWriter.java @@ -1,7 +1,6 @@ package com.alibaba.excel; import java.io.Closeable; -import java.io.IOException; import java.util.Collection; 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.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 * functions. @@ -29,7 +25,7 @@ import org.slf4j.LoggerFactory; */ public class ExcelWriter implements Closeable { - private ExcelBuilder excelBuilder; + private final ExcelBuilder excelBuilder; /** * Create new writer diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java index 0f7f707a..a1b7625d 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java @@ -1,7 +1,5 @@ package com.alibaba.excel.analysis.csv; -import java.io.FileInputStream; -import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; @@ -38,8 +36,8 @@ import org.apache.commons.csv.CSVRecord; @Slf4j public class CsvExcelReadExecutor implements ExcelReadExecutor { - private List sheetList; - private CsvReadContext csvReadContext; + private final List sheetList; + private final CsvReadContext csvReadContext; public CsvExcelReadExecutor(CsvReadContext csvReadContext) { this.csvReadContext = csvReadContext; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java index cfdc6bee..14565e5e 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsListSheetListener.java +++ b/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 */ public class XlsListSheetListener implements HSSFListener { - private XlsReadContext xlsReadContext; + private final XlsReadContext xlsReadContext; private static final Map XLS_RECORD_HANDLER_MAP = new HashMap(); static { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java index 422ad090..e1a303b1 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v03/XlsSaxAnalyser.java +++ b/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 short DUMMY_RECORD_SID = -1; - private XlsReadContext xlsReadContext; + private final XlsReadContext xlsReadContext; private static final Map XLS_RECORD_HANDLER_MAP = new HashMap(32); static { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java index 50bd55a7..38918fb8 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java @@ -52,13 +52,13 @@ import org.xml.sax.XMLReader; @Slf4j public class XlsxSaxAnalyser implements ExcelReadExecutor { - private XlsxReadContext xlsxReadContext; - private List sheetList; - private Map sheetMap; + private final XlsxReadContext xlsxReadContext; + private final List sheetList; + private final Map sheetMap; /** * excel comments key: sheetNo value: CommentsTable */ - private Map commentsTableMap; + private final Map commentsTableMap; public XlsxSaxAnalyser(XlsxReadContext xlsxReadContext, InputStream decryptedStream) throws Exception { this.xlsxReadContext = xlsxReadContext; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java index ea6ffc58..f4514e43 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/SharedStringsTableHandler.java +++ b/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 ReadCache readCache; + private final ReadCache readCache; /** * Some fields in the T tag need to be ignored */ diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java index 869ef3cb..61805cd5 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/v07/handlers/sax/XlsxRowHandler.java +++ b/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 public class XlsxRowHandler extends DefaultHandler { - private XlsxReadContext xlsxReadContext; + private final XlsxReadContext xlsxReadContext; private static final Map XLSX_CELL_HANDLER_MAP = new HashMap(32); static { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java index f83a1233..82ada960 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/MapCache.java @@ -11,7 +11,7 @@ import com.alibaba.excel.context.AnalysisContext; * @author Jiaju Zhuang */ public class MapCache implements ReadCache { - private List cache = new ArrayList<>(); + private final List cache = new ArrayList<>(); @Override public void init(AnalysisContext analysisContext) {} diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java index 9e814e0f..261bc187 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/XlsCache.java @@ -11,7 +11,7 @@ import com.alibaba.excel.context.AnalysisContext; * @author Jiaju Zhuang */ public class XlsCache implements ReadCache { - private SSTRecord sstRecord; + private final SSTRecord sstRecord; public XlsCache(SSTRecord sstRecord) { this.sstRecord = sstRecord; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java index d7cedd07..658806bd 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/selector/SimpleReadCacheSelector.java @@ -33,12 +33,12 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { /** * 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. */ - private int maxCacheActivateSize; + private final int maxCacheActivateSize; public SimpleReadCacheSelector() { this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java b/easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java index 4edaef37..5385831b 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/context/AnalysisContextImpl.java @@ -46,7 +46,7 @@ public class AnalysisContextImpl implements AnalysisContext { /** * Event processor */ - private AnalysisEventProcessor analysisEventProcessor; + private final AnalysisEventProcessor analysisEventProcessor; public AnalysisContextImpl(ReadWorkbook readWorkbook, ExcelTypeEnum actualExcelType) { if (readWorkbook == null) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java index d3bf472b..d2a24b44 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java @@ -28,7 +28,7 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder List doReadAllSync() { SyncReadListener syncReadListener = new SyncReadListener(); registerReadListener(syncReadListener); - ExcelReader excelReader = build(); - excelReader.readAll(); - excelReader.finish(); + try (ExcelReader excelReader = build()) { + excelReader.readAll(); + excelReader.finish(); + } return (List)syncReadListener.getList(); } diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java index 84ec6bae..4778169b 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/read/builder/ExcelReaderSheetBuilder.java @@ -18,7 +18,7 @@ public class ExcelReaderSheetBuilder extends AbstractExcelReaderParameterBuilder /** * Sheet */ - private ReadSheet readSheet; + private final ReadSheet readSheet; public ExcelReaderSheetBuilder() { this.readSheet = new ReadSheet(); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java b/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java index 5923403b..90ebe2b5 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java +++ b/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.support.ExcelTypeEnum; -import com.alibaba.excel.util.StringUtils; - import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; -import org.apache.commons.compress.utils.CharsetNames; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java index d6503c6c..67e2b9a2 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/ExcelBuilderImpl.java +++ b/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 { - private WriteContext context; + private final WriteContext context; private ExcelWriteFillExecutor excelWriteFillExecutor; private ExcelWriteAddExecutor excelWriteAddExecutor; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java index f90e6cd7..75d702f8 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterBuilder.java @@ -6,7 +6,6 @@ import java.io.OutputStream; import java.nio.charset.Charset; import com.alibaba.excel.ExcelWriter; -import com.alibaba.excel.read.builder.ExcelReaderBuilder; import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.write.metadata.WriteWorkbook; diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java index 111c54e1..b7fd6805 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterSheetBuilder.java @@ -18,7 +18,7 @@ public class ExcelWriterSheetBuilder extends AbstractExcelWriterParameterBuilder /** * Sheet */ - private WriteSheet writeSheet; + private final WriteSheet writeSheet; public ExcelWriterSheetBuilder() { this.writeSheet = new WriteSheet(); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java index 279ac30a..6574db04 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/builder/ExcelWriterTableBuilder.java @@ -21,7 +21,7 @@ public class ExcelWriterTableBuilder extends AbstractExcelWriterParameterBuilder /** * table */ - private WriteTable writeTable; + private final WriteTable writeTable; public ExcelWriterTableBuilder() { this.writeTable = new WriteTable(); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java index 961db51f..79db35b7 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/LoopMergeStrategy.java @@ -15,15 +15,15 @@ public class LoopMergeStrategy implements RowWriteHandler { /** * Each row */ - private int eachRow; + private final int eachRow; /** * Extend column */ - private int columnExtend; + private final int columnExtend; /** * The number of the current column */ - private int columnIndex; + private final int columnIndex; public LoopMergeStrategy(int eachRow, int columnIndex) { this(eachRow, 1, columnIndex); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java index bd7b1836..273d8879 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/write/merge/OnceAbsoluteMergeStrategy.java @@ -16,19 +16,19 @@ public class OnceAbsoluteMergeStrategy implements SheetWriteHandler { /** * First row */ - private int firstRowIndex; + private final int firstRowIndex; /** * Last row */ - private int lastRowIndex; + private final int lastRowIndex; /** * First column */ - private int firstColumnIndex; + private final int firstColumnIndex; /** * Last row */ - private int lastColumnIndex; + private final int lastColumnIndex; public OnceAbsoluteMergeStrategy(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex) { if (firstRowIndex < 0 || lastRowIndex < 0 || firstColumnIndex < 0 || lastColumnIndex < 0) { diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java index 316ff2c8..5db857d5 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/style/column/SimpleColumnWidthStyleStrategy.java +++ b/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 */ public class SimpleColumnWidthStyleStrategy extends AbstractHeadColumnWidthStyleStrategy { - private Integer columnWidth; + private final Integer columnWidth; /** * diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java b/easyexcel-core/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java index 313cf21c..028707f3 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/write/style/row/SimpleRowHeightStyleStrategy.java +++ b/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 */ public class SimpleRowHeightStyleStrategy extends AbstractRowHeightStyleStrategy { - private Short headRowHeight; - private Short contentRowHeight; + private final Short headRowHeight; + private final Short contentRowHeight; public SimpleRowHeightStyleStrategy(Short headRowHeight, Short contentRowHeight) { this.headRowHeight = headRowHeight; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/charset/CharsetDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/charset/CharsetDataTest.java index 7da5b311..7ac39173 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/charset/CharsetDataTest.java +++ b/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.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; import java.util.List; 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.excel.EasyExcel; import com.alibaba.excel.context.AnalysisContext; 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.util.ConverterUtils; -import com.alibaba.excel.util.ListUtils; -import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.compress.utils.Charsets; import org.apache.commons.compress.utils.Lists; import org.junit.Assert; import org.junit.BeforeClass; diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java index 0a71d25a..fc353ba2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/converter/ConverterDataListener.java @@ -21,7 +21,7 @@ import org.slf4j.LoggerFactory; */ public class ConverterDataListener extends AnalysisEventListener { private static final Logger LOGGER = LoggerFactory.getLogger(ConverterDataListener.class); - private List list = new ArrayList<>(); + private final List list = new ArrayList<>(); @Override public void invoke(ConverterReadData data, AnalysisContext context) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java index e85d63a6..90772691 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/FillDataTest.java @@ -142,20 +142,20 @@ public class FillDataTest { } private void compositeFill(File file, File template) { - ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - - FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); - excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); - excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); - excelWriter.fill(new FillWrapper("data2", data()), writeSheet); - excelWriter.fill(new FillWrapper("data2", data()), writeSheet); - excelWriter.fill(new FillWrapper("data3", data()), writeSheet); - excelWriter.fill(new FillWrapper("data3", data()), writeSheet); - Map map = new HashMap(); - map.put("date", "2019年10月9日13:28:28"); - excelWriter.fill(map, writeSheet); - excelWriter.finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + + FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); + excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); + excelWriter.fill(new FillWrapper("data1", data()), fillConfig, writeSheet); + excelWriter.fill(new FillWrapper("data2", data()), writeSheet); + excelWriter.fill(new FillWrapper("data2", data()), writeSheet); + excelWriter.fill(new FillWrapper("data3", data()), writeSheet); + excelWriter.fill(new FillWrapper("data3", data()), writeSheet); + Map map = new HashMap(); + map.put("date", "2019年10月9日13:28:28"); + excelWriter.fill(map, writeSheet); + } List list = EasyExcel.read(file).ignoreEmptyRow(false).sheet().headRowNumber(0).doReadSync(); Map map0 = (Map)list.get(0); @@ -167,15 +167,16 @@ public class FillDataTest { } private void horizontalFill(File file, File template) { - ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); - excelWriter.fill(data(), fillConfig, writeSheet); - excelWriter.fill(data(), fillConfig, writeSheet); - Map map = new HashMap(); - map.put("date", "2019年10月9日13:28:28"); - excelWriter.fill(map, writeSheet); - excelWriter.finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build(); + excelWriter.fill(data(), fillConfig, writeSheet); + excelWriter.fill(data(), fillConfig, writeSheet); + Map map = new HashMap(); + map.put("date", "2019年10月9日13:28:28"); + excelWriter.fill(map, writeSheet); + excelWriter.finish(); + } List list = EasyExcel.read(file).sheet().headRowNumber(0).doReadSync(); Assert.assertEquals(list.size(), 5L); @@ -184,16 +185,16 @@ public class FillDataTest { } private void complexFill(File file, File template) { - ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build(); - FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); - excelWriter.fill(data(), fillConfig, writeSheet); - excelWriter.fill(data(), fillConfig, writeSheet); - Map map = new HashMap(); - map.put("date", "2019年10月9日13:28:28"); - map.put("total", 1000); - excelWriter.fill(map, writeSheet); - excelWriter.finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file).withTemplate(template).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet().registerWriteHandler(new LoopMergeStrategy(2, 0)).build(); + FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); + excelWriter.fill(data(), fillConfig, writeSheet); + excelWriter.fill(data(), fillConfig, writeSheet); + Map map = new HashMap(); + map.put("date", "2019年10月9日13:28:28"); + map.put("total", 1000); + excelWriter.fill(map, writeSheet); + } List list = EasyExcel.read(file).sheet().headRowNumber(3).doReadSync(); Assert.assertEquals(list.size(), 21L); Map map19 = (Map)list.get(19); diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java index 2106c0ef..8d1d56bc 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/fill/annotation/FillAnnotationDataTest.java @@ -61,46 +61,47 @@ public class FillAnnotationDataTest { private void readAndWrite(File file, File fileTemplate) throws Exception { EasyExcel.write().file(file).head(FillAnnotationData.class).withTemplate(fileTemplate).sheet().doFill(data()); - Workbook workbook = WorkbookFactory.create(file); - Sheet sheet = workbook.getSheetAt(0); + try (Workbook workbook = WorkbookFactory.create(file)) { + Sheet sheet = workbook.getSheetAt(0); - Row row1 = sheet.getRow(1); - Assert.assertEquals(2000, row1.getHeight(), 0); - Cell cell10 = row1.getCell(0); - Date date = cell10.getDateCellValue(); - Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime()); - String dataFormatString = cell10.getCellStyle().getDataFormatString(); - Assert.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString); - Cell cell11 = row1.getCell(1); - Assert.assertEquals(99.99, cell11.getNumericCellValue(), 2); - boolean hasMerge = false; - for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { - if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1 - && mergedRegion.getFirstColumn() == 2 && mergedRegion.getLastColumn() == 3) { - hasMerge = true; + Row row1 = sheet.getRow(1); + Assert.assertEquals(2000, row1.getHeight(), 0); + Cell cell10 = row1.getCell(0); + Date date = cell10.getDateCellValue(); + Assert.assertEquals(DateUtils.parseDate("2020-01-01 01:01:01").getTime(), date.getTime()); + String dataFormatString = cell10.getCellStyle().getDataFormatString(); + Assert.assertEquals("yyyy年MM月dd日HH时mm分ss秒", dataFormatString); + Cell cell11 = row1.getCell(1); + Assert.assertEquals(99.99, cell11.getNumericCellValue(), 2); + boolean hasMerge = false; + for (CellRangeAddress mergedRegion : sheet.getMergedRegions()) { + if (mergedRegion.getFirstRow() == 1 && mergedRegion.getLastRow() == 1 + && mergedRegion.getFirstColumn() == 2 && mergedRegion.getLastColumn() == 3) { + hasMerge = true; + break; + } + } + Assert.assertTrue(hasMerge); + if (sheet instanceof XSSFSheet) { + XSSFSheet xssfSheet = (XSSFSheet)sheet; + List shapeList = xssfSheet.getDrawingPatriarch().getShapes(); + XSSFShape shape0 = shapeList.get(0); + Assert.assertTrue(shape0 instanceof XSSFPicture); + XSSFPicture picture0 = (XSSFPicture)shape0; + CTMarker ctMarker0 = picture0.getPreferredSize().getFrom(); + Assert.assertEquals(1, ctMarker0.getRow()); + Assert.assertEquals(4, ctMarker0.getCol()); + } else { + HSSFSheet hssfSheet = (HSSFSheet)sheet; + List shapeList = hssfSheet.getDrawingPatriarch().getChildren(); + HSSFShape shape0 = shapeList.get(0); + Assert.assertTrue(shape0 instanceof HSSFPicture); + HSSFPicture picture0 = (HSSFPicture)shape0; + HSSFClientAnchor anchor = (HSSFClientAnchor)picture0.getAnchor(); + Assert.assertEquals(1, anchor.getRow1()); + Assert.assertEquals(4, anchor.getCol1()); } } - Assert.assertTrue(hasMerge); - if (sheet instanceof XSSFSheet) { - XSSFSheet xssfSheet = (XSSFSheet)sheet; - List shapeList = xssfSheet.getDrawingPatriarch().getShapes(); - XSSFShape shape0 = shapeList.get(0); - Assert.assertTrue(shape0 instanceof XSSFPicture); - XSSFPicture picture0 = (XSSFPicture)shape0; - CTMarker ctMarker0 = picture0.getPreferredSize().getFrom(); - Assert.assertEquals(1, ctMarker0.getRow()); - Assert.assertEquals(4, ctMarker0.getCol()); - } else { - HSSFSheet hssfSheet = (HSSFSheet)sheet; - List shapeList = hssfSheet.getDrawingPatriarch().getChildren(); - HSSFShape shape0 = shapeList.get(0); - Assert.assertTrue(shape0 instanceof HSSFPicture); - HSSFPicture picture0 = (HSSFPicture)shape0; - HSSFClientAnchor anchor = (HSSFClientAnchor)picture0.getAnchor(); - Assert.assertEquals(1, anchor.getRow1()); - Assert.assertEquals(4, anchor.getCol1()); - } - } private List data() throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index c7f202e5..d8acbba5 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -57,26 +57,26 @@ public class LargeDataTest { @Test public void t02Fill() { - ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - for (int j = 0; j < 5000; j++) { - excelWriter.fill(data(), writeSheet); - LOGGER.info("{} fill success.", j); + try (ExcelWriter excelWriter = EasyExcel.write(fileFill07).withTemplate(template07).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 5000; j++) { + excelWriter.fill(data(), writeSheet); + LOGGER.info("{} fill success.", j); + } } - excelWriter.finish(); } @Test public void t03ReadAndWriteCsv() { // write long start = System.currentTimeMillis(); - ExcelWriter excelWriter = EasyExcel.write(fileCsv).build(); - WriteSheet writeSheet = EasyExcel.writerSheet().build(); - for (int j = 0; j < 5000; j++) { - excelWriter.write(data(), writeSheet); - LOGGER.info("{} write success.", j); + try (ExcelWriter excelWriter = EasyExcel.write(fileCsv).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet().build(); + for (int j = 0; j < 5000; j++) { + excelWriter.write(data(), writeSheet); + LOGGER.info("{} write success.", j); + } } - excelWriter.finish(); LOGGER.info("CSV large data total time spent:{}", System.currentTimeMillis() - start); // read diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java index c69e3ae4..b87decde 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/multiplesheets/MultipleSheetsDataTest.java +++ b/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; /** - * * @author Jiaju Zhuang */ @FixMethodOrder(MethodSorters.NAME_ASCENDING) @@ -52,15 +51,15 @@ public class MultipleSheetsDataTest { private void read(File file) { MultipleSheetsListener multipleSheetsListener = new MultipleSheetsListener(); - ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build(); - List sheets = excelReader.excelExecutor().sheetList(); - int count = 1; - for (ReadSheet readSheet : sheets) { - excelReader.read(readSheet); - Assert.assertEquals(multipleSheetsListener.getList().size(), count); - count++; + try (ExcelReader excelReader = EasyExcel.read(file, MultipleSheetsData.class, multipleSheetsListener).build()) { + List sheets = excelReader.excelExecutor().sheetList(); + int count = 1; + for (ReadSheet readSheet : sheets) { + excelReader.read(readSheet); + Assert.assertEquals(multipleSheetsListener.getList().size(), count); + count++; + } } - excelReader.finish(); } private void readAll(File file) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java index 70acf19f..7314c67c 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/repetition/RepetitionDataTest.java +++ b/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) { - ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build(); - WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); - excelWriter.write(data(), writeSheet).write(data(), writeSheet).finish(); - ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build(); - ReadSheet readSheet = EasyExcel.readSheet(0).build(); - excelReader.read(readSheet).finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); + excelWriter.write(data(), writeSheet).write(data(), writeSheet); + } + try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()) + .build()) { + ReadSheet readSheet = EasyExcel.readSheet(0).build(); + excelReader.read(readSheet); + } } @Test @@ -80,13 +83,16 @@ public class RepetitionDataTest { } private void readAndWriteTable(File file) { - ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build(); - WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); - WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build(); - excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable).finish(); - ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()).build(); - ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build(); - excelReader.read(readSheet).finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file, RepetitionData.class).build()) { + WriteSheet writeSheet = EasyExcel.writerSheet(0).build(); + WriteTable writeTable = EasyExcel.writerTable(0).relativeHeadRowIndex(0).build(); + excelWriter.write(data(), writeSheet, writeTable).write(data(), writeSheet, writeTable); + } + try (ExcelReader excelReader = EasyExcel.read(file, RepetitionData.class, new RepetitionDataListener()) + .build()) { + ReadSheet readSheet = EasyExcel.readSheet(0).headRowNumber(2).build(); + excelReader.read(readSheet); + } } private List data() { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java index ed9e84df..0d0777e2 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/skip/SkipDataTest.java @@ -53,32 +53,32 @@ public class SkipDataTest { } private void readAndWrite(File file) { - ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build(); - WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build(); - WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build(); - WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build(); - WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "第四个").build(); - excelWriter.write(data("name1"), writeSheet0); - excelWriter.write(data("name2"), writeSheet1); - excelWriter.write(data("name3"), writeSheet2); - excelWriter.write(data("name4"), writeSheet3); - excelWriter.finish(); + try (ExcelWriter excelWriter = EasyExcel.write(file, SimpleData.class).build();) { + WriteSheet writeSheet0 = EasyExcel.writerSheet(0, "第一个").build(); + WriteSheet writeSheet1 = EasyExcel.writerSheet(1, "第二个").build(); + WriteSheet writeSheet2 = EasyExcel.writerSheet(2, "第三个").build(); + WriteSheet writeSheet3 = EasyExcel.writerSheet(3, "第四个").build(); + excelWriter.write(data("name1"), writeSheet0); + excelWriter.write(data("name2"), writeSheet1); + excelWriter.write(data("name3"), writeSheet2); + excelWriter.write(data("name4"), writeSheet3); + } List list = EasyExcel.read(file, SkipData.class, null).sheet("第二个").doReadSync(); Assert.assertEquals(1, list.size()); Assert.assertEquals("name2", list.get(0).getName()); SyncReadListener syncReadListener = new SyncReadListener(); - ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener) - .build(); - ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build(); - ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build(); - excelReader.read(readSheet1, readSheet3); - List syncList = syncReadListener.getList(); - Assert.assertEquals(2, syncList.size()); - Assert.assertEquals("name2", ((SkipData)syncList.get(0)).getName()); - Assert.assertEquals("name4", ((SkipData)syncList.get(1)).getName()); - excelReader.finish(); + try (ExcelReader excelReader = EasyExcel.read(file, SkipData.class, null).registerReadListener(syncReadListener) + .build()) { + ReadSheet readSheet1 = EasyExcel.readSheet("第二个").build(); + ReadSheet readSheet3 = EasyExcel.readSheet("第四个").build(); + excelReader.read(readSheet1, readSheet3); + List syncList = syncReadListener.getList(); + Assert.assertEquals(2, syncList.size()); + Assert.assertEquals("name2", ((SkipData)syncList.get(0)).getName()); + Assert.assertEquals("name4", ((SkipData)syncList.get(1)).getName()); + } } private List data(String name) { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java index e3237053..f7d015ca 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/demo/write/WriteTest.java @@ -216,7 +216,7 @@ public class WriteTest { } finally { // 千万别忘记close 会帮忙关闭流 if (writer != null) { - writer.finish(); + writer.close(); } } @@ -250,7 +250,7 @@ public class WriteTest { } finally { // 千万别忘记close 会帮忙关闭流 if (writer != null) { - writer.finish(); + writer.close(); } } @@ -286,7 +286,7 @@ public class WriteTest { } finally { // 千万别忘记close 会帮忙关闭流 if (writer != null) { - writer.finish(); + writer.close(); } } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java index 23f623b6..aa772c20 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/WriteLargeTest.java @@ -28,7 +28,6 @@ import org.slf4j.LoggerFactory; public class WriteLargeTest { private static final Logger LOGGER = LoggerFactory.getLogger(WriteLargeTest.class); - private int i = 0; @Test public void test() throws Exception { diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443.java index 45309c9f..e5a87c5f 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/temp/issue2443/Issue2443.java +++ b/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.Setter; -import java.util.Date; - @Getter @Setter @EqualsAndHashCode