From 11bea4775815ea1608ff7e68e9a84b81e07cc095 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Thu, 10 Feb 2022 21:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E6=96=87=E4=BB=B6=E6=B5=81=E5=88=A4=E6=96=ADcsv?= =?UTF-8?q?=E7=9A=84bug=20[Issue=20#2297]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../excel/analysis/ExcelAnalyserImpl.java | 13 +++++++++ .../analysis/csv/CsvExcelReadExecutor.java | 10 ++++--- .../holder/csv/CsvReadWorkbookHolder.java | 3 +++ .../test/temp/write/TempWriteData.java | 2 ++ .../test/temp/write/TempWriteTest.java | 27 +++++++++++++++++++ update.md | 2 ++ 6 files changed, 53 insertions(+), 4 deletions(-) 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 743daab9a..161c29434 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.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 void finish() { } 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 d411ef0d1..2c35b105d 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 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 List sheetList() { @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 void execute() { int rowIndex = 0; - for (CSVRecord record : parseRecords) { + for (CSVRecord record : csvParser) { dealRecord(record, rowIndex++); } @@ -77,7 +79,7 @@ public void execute() { } } - 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 0a322d584..0471bcf02 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.Getter; import lombok.Setter; import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVParser; /** * Workbook holder @@ -20,6 +21,8 @@ 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 b26ce4b6f..2ef4b56ae 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 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 0f218e454..6e23c0e0b 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 void write() { .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 66f1c9774..33bce517b 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` 转换异常不抛出的问题