From 2bc7baa93098dc7bbf83e04aa30558bf69302990 Mon Sep 17 00:00:00 2001 From: cc323110 Date: Thu, 16 Dec 2021 01:26:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(CSV=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90):?= =?UTF-8?q?=20=E6=96=B0=E5=A2=9E=E4=BA=86=E8=AE=BE=E7=BD=AE=E7=BC=96?= =?UTF-8?q?=E7=A0=81=E7=9A=84=E5=8A=9F=E8=83=BD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解析CSV文件时,编码不正确会导致解析内容为乱码. --- .../com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java | 4 ++-- .../com/alibaba/excel/read/builder/ExcelReaderBuilder.java | 5 +++++ .../java/com/alibaba/excel/read/metadata/ReadWorkbook.java | 2 ++ .../read/metadata/holder/csv/CsvReadWorkbookHolder.java | 5 +++++ 4 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java b/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java index d411ef0d..cb0af033 100644 --- a/src/main/java/com/alibaba/excel/analysis/csv/CsvExcelReadExecutor.java +++ b/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) { diff --git a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java index b630eea2..0cb11009 100644 --- a/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java +++ b/src/main/java/com/alibaba/excel/read/builder/ExcelReaderBuilder.java @@ -38,6 +38,11 @@ public class ExcelReaderBuilder extends AbstractExcelReaderParameterBuilder diff --git a/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java index 54ab034c..eb9c9b86 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java +++ b/src/main/java/com/alibaba/excel/read/metadata/ReadWorkbook.java @@ -98,4 +98,6 @@ public class ReadWorkbook extends ReadBasicParameter { * @see CellExtraTypeEnum */ private Set extraReadSet; + + private String encoding; } diff --git a/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java b/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java index 0a322d58..b83b3bc8 100644 --- a/src/main/java/com/alibaba/excel/read/metadata/holder/csv/CsvReadWorkbookHolder.java +++ b/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(); } }