Browse Source

修复无法根据文件流判断csv的bug [Issue #2297]

pull/2317/head
Jiaju Zhuang 3 years ago
parent
commit
11bea47758
  1. 13
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/ExcelAnalyserImpl.java
  2. 10
      easyexcel-core/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  3. 3
      easyexcel-core/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java
  4. 2
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteData.java
  5. 27
      easyexcel-core/src/test/java/com/alibaba/easyexcel/test/temp/write/TempWriteTest.java
  6. 2
      update.md

13
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) {

10
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<CSVRecord> 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<CSVRecord> parseRecords() throws IOException {
private CSVParser csvParser() throws IOException {
CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder();
CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat();

3
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);

2
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)

27
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<String, String> map = new HashMap<>();
map.put("name", "zs");
BeanMap beanMap2 = BeanMapUtils.create(tempWriteData2);
beanMap2.putAll(map);
log.info("3{}", tempWriteData2.getName());
}
}

2
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` 转换异常不抛出的问题

Loading…
Cancel
Save