Browse Source

Merge pull request #2230 from cc323110/master

feat(CSV文件解析): 新增了设置编码的功能.
temp_merge
Jiaju Zhuang 3 years ago committed by GitHub
parent
commit
0fb3b915b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java
  2. 5
      src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java
  3. 2
      src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java
  4. 5
      src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java

4
src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java

@ -82,12 +82,12 @@ public class CsvExcelReadExecutor implements ExcelReadExecutor {
CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat();
if (csvReadWorkbookHolder.getMandatoryUseInputStream()) {
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream()));
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream(),csvReadWorkbookHolder.getEncoding()));
}
if (csvReadWorkbookHolder.getFile() != null) {
return csvFormat.parse(new FileReader(csvReadWorkbookHolder.getFile()));
}
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream()));
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream(),csvReadWorkbookHolder.getEncoding()));
}
private void dealRecord(CSVRecord record, int rowIndex) {

5
src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java

@ -38,6 +38,11 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder<Exce
return this;
}
public ExcelReaderBuilder encoding(String encoding){
readWorkbook.setEncoding(encoding);
return this;
}
/**
* Read InputStream
* <p>

2
src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java

@ -98,4 +98,6 @@ public class ReadWorkbook extends ReadBasicParameter {
* @see CellExtraTypeEnum
*/
private Set<CellExtraTypeEnum> extraReadSet;
private String encoding;
}

5
src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java

@ -4,9 +4,11 @@ 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;
/**
@ -21,9 +23,12 @@ public class CsvReadWorkbookHolder extends ReadWorkbookHolder {
private CSVFormat csvFormat;
private String encoding;
public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook);
setExcelType(ExcelTypeEnum.CSV);
this.csvFormat = CSVFormat.DEFAULT;
this.encoding = StringUtils.isEmpty(readWorkbook.getEncoding())? CharsetNames.UTF_8:readWorkbook.getEncoding();
}
}

Loading…
Cancel
Save