diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java index 743daab9..161c2943 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java @@ -18,6 +18,7 @@ import com.alibaba.excel.exception.ExcelAnalysisStopException; import com.alibaba.excel.read.metadata.ReadSheet; import com.alibaba.excel.read.metadata.ReadWorkbook; import com.alibaba.excel.read.metadata.holder.ReadWorkbookHolder; +import com.alibaba.excel.read.metadata.holder.csv.CsvReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xls.XlsReadWorkbookHolder; import com.alibaba.excel.read.metadata.holder.xlsx.XlsxReadWorkbookHolder; import com.alibaba.excel.support.ExcelTypeEnum; @@ -171,6 +172,18 @@ public class ExcelAnalyserImpl implements ExcelAnalyser { } catch (Throwable t) { throwable = t; } + + // close csv + // https://github.com/alibaba/easyexcel/issues/2309 + try { + if ((readWorkbookHolder instanceof CsvReadWorkbookHolder) + && ((CsvReadWorkbookHolder)readWorkbookHolder).getCsvParser() != null) { + ((CsvReadWorkbookHolder)readWorkbookHolder).getCsvParser().close(); + } + } catch (Throwable t) { + throwable = t; + } + try { if (analysisContext.readWorkbookHolder().getAutoCloseStream() && readWorkbookHolder.getInputStream() != null) { 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 d411ef0d..2c35b105 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 @@ -25,6 +25,7 @@ import com.alibaba.excel.util.StringUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.MapUtils; import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; /** @@ -53,9 +54,10 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { @Override public void execute() { - Iterable parseRecords; + CSVParser csvParser; try { - parseRecords = parseRecords(); + csvParser = csvParser(); + csvReadContext.csvReadWorkbookHolder().setCsvParser(csvParser); } catch (IOException e) { throw new ExcelAnalysisException(e); } @@ -68,7 +70,7 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { int rowIndex = 0; - for (CSVRecord record : parseRecords) { + for (CSVRecord record : csvParser) { dealRecord(record, rowIndex++); } @@ -77,7 +79,7 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor { } } - private Iterable parseRecords() throws IOException { + private CSVParser csvParser() throws IOException { CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder(); CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat(); 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 0a322d58..0471bcf0 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 @@ -8,6 +8,7 @@ import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; /** * Workbook holder @@ -20,6 +21,8 @@ import org.apache.commons.csv.CSVFormat; public class CsvReadWorkbookHolder extends ReadWorkbookHolder { private CSVFormat csvFormat; + private CSVParser csvParser; + public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) { super(readWorkbook); diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java index b26ce4b6..2ef4b56a 100644 --- a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java +++ b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java @@ -8,7 +8,9 @@ import com.alibaba.excel.enums.BooleanEnum; import lombok.Data; @Data +//@Accessors(chain = true) public class TempWriteData { + private String name1; @ExcelProperty(" 换行\r\n \\ \r\n的名字") @HeadStyle(wrapped = BooleanEnum.TRUE) diff --git a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java index 0f218e45..6e23c0e0 100644 --- a/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java +++ b/easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java @@ -1,13 +1,20 @@ package com.alibaba.easyexcel.test.temp.write; +import java.util.HashMap; +import java.util.Map; + import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.util.BeanMapUtils; import com.alibaba.excel.util.ListUtils; +import lombok.extern.slf4j.Slf4j; import org.junit.Ignore; import org.junit.Test; +import org.springframework.cglib.beans.BeanMap; @Ignore +@Slf4j public class TempWriteTest { @Test @@ -19,4 +26,24 @@ public class TempWriteTest { .sheet() .doWrite(ListUtils.newArrayList(tempWriteData)); } + + @Test + public void cglib() { + TempWriteData tempWriteData = new TempWriteData(); + tempWriteData.setName("1"); + tempWriteData.setName1("2"); + BeanMap beanMap = BeanMapUtils.create(tempWriteData); + + log.info("d1{}", beanMap.get("name")); + log.info("d2{}", beanMap.get("name1")); + + TempWriteData tempWriteData2 = new TempWriteData(); + + Map map = new HashMap<>(); + map.put("name", "zs"); + BeanMap beanMap2 = BeanMapUtils.create(tempWriteData2); + beanMap2.putAll(map); + log.info("3{}", tempWriteData2.getName()); + + } } diff --git a/update.md b/update.md index 66f1c977..33bce517 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,8 @@ * 支持jdk17,去除cglib&asm依赖,改成重新拷贝一份 [Issue #2240](https://github.com/alibaba/easyexcel/issues/2240) * 在有样式没有数据的情况下也算空行 [Issue #2294](https://github.com/alibaba/easyexcel/issues/2294) * 修复无法根据文件流判断csv的bug [Issue #2297](https://github.com/alibaba/easyexcel/issues/2297) +* 修复CSV不关闭流的bug [Issue #2309](https://github.com/alibaba/easyexcel/issues/2309) + # 3.0.5 * 修复`ReadListener` 转换异常不抛出的问题