Skip to content

Commit

Permalink
Merge pull request #69 from liaochong/Hotfix/2.3.3
Browse files Browse the repository at this point in the history
Hotfix/2.3.3
  • Loading branch information
liaochong authored May 29, 2019
2 parents 47098f3 + 0a22edc commit 454b4bc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<groupId>com.github.liaochong</groupId>
<artifactId>myexcel</artifactId>
<version>2.3.2</version>
<version>2.3.3</version>
<packaging>jar</packaging>

<name>myexcel</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class DefaultExcelReader<T> {

private boolean parallelRead;

private Workbook wb;

private DefaultExcelReader(Class<T> dataType) {
this.dataType = dataType;
}
Expand Down Expand Up @@ -102,8 +104,14 @@ public List<T> read(@NonNull InputStream fileInputStream, String password) throw
if (fieldMap.isEmpty()) {
return Collections.emptyList();
}
Sheet sheet = getSheetOfInputStream(fileInputStream, password);
return getDataFromFile(sheet, fieldMap);
try {
Sheet sheet = getSheetOfInputStream(fileInputStream, password);
return getDataFromFile(sheet, fieldMap);
} finally {
if (Objects.nonNull(wb)) {
wb.close();
}
}
}

public List<T> read(@NonNull File file) throws Exception {
Expand All @@ -118,8 +126,14 @@ public List<T> read(@NonNull File file, String password) throws Exception {
if (fieldMap.isEmpty()) {
return Collections.emptyList();
}
Sheet sheet = getSheetOfFile(file, password);
return getDataFromFile(sheet, fieldMap);
try {
Sheet sheet = getSheetOfFile(file, password);
return getDataFromFile(sheet, fieldMap);
} finally {
if (Objects.nonNull(wb)) {
wb.close();
}
}
}

public void readThen(@NonNull InputStream fileInputStream, Consumer<T> consumer) throws Exception {
Expand All @@ -131,8 +145,14 @@ public void readThen(@NonNull InputStream fileInputStream, String password, Cons
if (fieldMap.isEmpty()) {
return;
}
Sheet sheet = getSheetOfInputStream(fileInputStream, password);
readThenConsume(sheet, fieldMap, consumer);
try {
Sheet sheet = getSheetOfInputStream(fileInputStream, password);
readThenConsume(sheet, fieldMap, consumer);
} finally {
if (Objects.nonNull(wb)) {
wb.close();
}
}
}


Expand All @@ -148,12 +168,17 @@ public void readThen(@NonNull File file, String password, Consumer<T> consumer)
if (fieldMap.isEmpty()) {
return;
}
Sheet sheet = getSheetOfFile(file, password);
readThenConsume(sheet, fieldMap, consumer);
try {
Sheet sheet = getSheetOfFile(file, password);
readThenConsume(sheet, fieldMap, consumer);
} finally {
if (Objects.nonNull(wb)) {
wb.close();
}
}
}

private Sheet getSheetOfInputStream(@NonNull InputStream fileInputStream, String password) throws IOException {
Workbook wb;
if (StringUtil.isBlank(password)) {
wb = WorkbookFactory.create(fileInputStream);
} else {
Expand All @@ -163,7 +188,6 @@ private Sheet getSheetOfInputStream(@NonNull InputStream fileInputStream, String
}

private Sheet getSheetOfFile(@NonNull File file, String password) throws IOException {
Workbook wb;
if (StringUtil.isBlank(password)) {
wb = WorkbookFactory.create(file);
} else {
Expand Down

0 comments on commit 454b4bc

Please sign in to comment.