Browse Source

feat(CSV文件解析): 新增了设置编码的功能.

解析CSV文件时,编码不正确会导致解析内容为乱码.
pull/2230/head
cc323110 3 years ago
parent
commit
2bc7baa930
  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(); CSVFormat csvFormat = csvReadWorkbookHolder.getCsvFormat();
if (csvReadWorkbookHolder.getMandatoryUseInputStream()) { if (csvReadWorkbookHolder.getMandatoryUseInputStream()) {
return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream())); return csvFormat.parse(new InputStreamReader(csvReadWorkbookHolder.getInputStream(),csvReadWorkbookHolder.getEncoding()));
} }
if (csvReadWorkbookHolder.getFile() != null) { if (csvReadWorkbookHolder.getFile() != null) {
return csvFormat.parse(new FileReader(csvReadWorkbookHolder.getFile())); 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) { 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; return this;
} }
public ExcelReaderBuilder encoding(String encoding){
readWorkbook.setEncoding(encoding);
return this;
}
/** /**
* Read InputStream * Read InputStream
* <p> * <p>

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

@ -98,4 +98,6 @@ public class ReadWorkbook extends ReadBasicParameter {
* @see CellExtraTypeEnum * @see CellExtraTypeEnum
*/ */
private Set<CellExtraTypeEnum> extraReadSet; 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.read.metadata.holder.ReadWorkbookHolder;
import com.alibaba.excel.support.ExcelTypeEnum; import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.StringUtils;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.compress.utils.CharsetNames;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
/** /**
@ -21,9 +23,12 @@ public class CsvReadWorkbookHolder extends ReadWorkbookHolder {
private CSVFormat csvFormat; private CSVFormat csvFormat;
private String encoding;
public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) { public CsvReadWorkbookHolder(ReadWorkbook readWorkbook) {
super(readWorkbook); super(readWorkbook);
setExcelType(ExcelTypeEnum.CSV); setExcelType(ExcelTypeEnum.CSV);
this.csvFormat = CSVFormat.DEFAULT; this.csvFormat = CSVFormat.DEFAULT;
this.encoding = StringUtils.isEmpty(readWorkbook.getEncoding())? CharsetNames.UTF_8:readWorkbook.getEncoding();
} }
} }

Loading…
Cancel
Save