Skip to content

Commit

Permalink
Merge pull request #119 from liaochong/Hotfix/2.8.4
Browse files Browse the repository at this point in the history
SaxExcelReader新增charset设置
  • Loading branch information
liaochong authored Aug 17, 2019
2 parents 5f4b469 + 66d2f93 commit d38a760
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>2.8.3</version>
<version>2.8.4</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/com/github/liaochong/myexcel/core/CsvHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
Expand Down Expand Up @@ -56,6 +55,8 @@ class CsvHandler<T> {

private Predicate<T> beanFilter;

private String charset;

public CsvHandler(InputStream is,
SaxExcelReader.ReadConfig<T> readConfig,
List<T> result) {
Expand All @@ -72,6 +73,7 @@ public CsvHandler(InputStream is,
this.function = readConfig.getFunction();
this.rowFilter = readConfig.getRowFilter();
this.beanFilter = readConfig.getBeanFilter();
this.charset = readConfig.getCharset();
}

public CsvHandler(File file,
Expand All @@ -86,6 +88,7 @@ public CsvHandler(File file,
this.function = readConfig.getFunction();
this.rowFilter = readConfig.getRowFilter();
this.beanFilter = readConfig.getBeanFilter();
this.charset = readConfig.getCharset();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand All @@ -96,7 +99,7 @@ public void read() {
return;
}
long startTime = System.currentTimeMillis();
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is))) {
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is, charset))) {
int lineIndex = 0;
String line;
while ((line = bufferedReader.readLine()) != null) {
Expand All @@ -119,11 +122,11 @@ private void process(String line, Row row) throws Exception {
}
T obj = dataType.newInstance();
if (line != null) {
String[] strArr = line.trim().split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)", -1);
String[] strArr = line.split(",(?=([^\\\"]*\\\"[^\\\"]*\\\")*[^\\\"]*$)", -1);
for (int i = 0, size = strArr.length; i < size; i++) {
String content = strArr[i];
Field field = fieldMap.get(i);
if (Objects.isNull(field)) {
if (field == null) {
continue;
}
ReadConverterContext.convert(content, field, obj);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public Workbook build(List<?> data, Class<?>... groups) {

Table table = this.createTable();
List<Tr> thead = this.createThead();
List<Tr> tbody = this.createTbody(contents, Objects.isNull(thead) ? 0 : thead.size());
if (Objects.nonNull(thead)) {
List<Tr> tbody = this.createTbody(contents, thead == null ? 0 : thead.size());
if (thead != null) {
tbody.addAll(0, thead);
}
table.setTrList(tbody);
Expand All @@ -127,15 +127,15 @@ public Workbook build(List<?> data, Class<?>... groups) {
tableList.add(table);

if (sortedFields.isEmpty()) {
if (Objects.nonNull(thead)) {
if (thead != null) {
table.getTrList().addAll(thead);
}
log.info("The specified field mapping does not exist");
return htmlToExcelFactory.build(tableList, workbook);
}

if (Objects.isNull(data) || data.isEmpty()) {
if (Objects.nonNull(thead)) {
if (data == null || data.isEmpty()) {
if (thead != null) {
table.getTrList().addAll(thead);
}
log.info("No valid data exists");
Expand All @@ -144,7 +144,7 @@ public Workbook build(List<?> data, Class<?>... groups) {

List<List<Pair<? extends Class, ?>>> contents = getRenderContent(data, sortedFields);
List<Tr> tbody = this.createTbody(contents, Objects.isNull(thead) ? 0 : thead.size());
if (Objects.nonNull(thead)) {
if (thead != null) {
tbody.addAll(0, thead);
}
table.setTrList(tbody);
Expand Down Expand Up @@ -179,7 +179,7 @@ private Workbook mapBuild(List<Map<String, Object>> data, HtmlToExcelFactory htm
List<Pair<? extends Class, ?>> contents = new ArrayList<>(d.size());
for (String fieldName : fieldDisplayOrder) {
Object val = d.get(fieldName);
contents.add(Pair.of(Objects.isNull(val) ? String.class : val.getClass(), val));
contents.add(Pair.of(val == null ? String.class : val.getClass(), val));
}
Tr tr = this.createTr(contents, i, thead.size());
if (widths != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public SaxExcelReader<T> beanFilter(Predicate<T> beanFilter) {
return this;
}

public SaxExcelReader<T> charset(String charset) {
this.readConfig.charset = charset;
return this;
}

public List<T> read(@NonNull InputStream fileInputStream) {
if (!fileInputStream.markSupported()) {
fileInputStream = new BufferedInputStream(fileInputStream);
Expand Down Expand Up @@ -331,5 +336,7 @@ public static class ReadConfig<T> {
private Predicate<Row> rowFilter = row -> true;

private Predicate<T> beanFilter = bean -> true;

private String charset = "UTF-8";
}
}

0 comments on commit d38a760

Please sign in to comment.