Skip to content

Commit

Permalink
修复无法根据文件流判断csv的bug [Issue #2297]
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuangjiaju committed Feb 10, 2022
1 parent 4263ba5 commit 11bea47
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -53,9 +54,10 @@ public List<ReadSheet> sheetList() {

@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);
}
Expand All @@ -68,7 +70,7 @@ public void execute() {

int rowIndex = 0;

for (CSVRecord record : parseRecords) {
for (CSVRecord record : csvParser) {
dealRecord(record, rowIndex++);
}

Expand All @@ -77,7 +79,7 @@ public void execute() {
}
}

private Iterable<CSVRecord> parseRecords() throws IOException {
private CSVParser csvParser() throws IOException {
CsvReadWorkbookHolder csvReadWorkbookHolder = csvReadContext.csvReadWorkbookHolder();
CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;

/**
* Workbook holder
Expand All @@ -20,6 +21,8 @@
public class CsvReadWorkbookHolder extends ReadWorkbookHolder {

private CSVFormat csvFormat;
private CSVParser csvParser;


public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<String, String> map = new HashMap<>();
map.put("name", "zs");
BeanMap beanMap2 = BeanMapUtils.create(tempWriteData2);
beanMap2.putAll(map);
log.info("3{}", tempWriteData2.getName());

}
}
2 changes: 2 additions & 0 deletions update.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` 转换异常不抛出的问题
Expand Down

0 comments on commit 11bea47

Please sign in to comment.