Skip to content

Commit 4d966a1

Browse files
authored
Merge pull request #257 from liaochong/Hotfix/3.9.1
1. 修复异常情况下消费者线程未被关闭问题
2 parents bdf5813 + 902d0c9 commit 4d966a1

6 files changed

Lines changed: 94 additions & 30 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<groupId>com.github.liaochong</groupId>
1313
<artifactId>myexcel</artifactId>
14-
<version>3.9.0</version>
14+
<version>3.9.1</version>
1515
<packaging>jar</packaging>
1616

1717
<name>myexcel</name>

src/main/java/com/github/liaochong/myexcel/core/DefaultStreamExcelBuilder.java

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@
2424
import com.github.liaochong.myexcel.core.strategy.WidthStrategy;
2525
import com.github.liaochong.myexcel.core.templatehandler.TemplateHandler;
2626
import com.github.liaochong.myexcel.utils.ReflectUtil;
27-
import lombok.NonNull;
27+
import com.github.liaochong.myexcel.utils.TempFileOperator;
2828
import lombok.extern.slf4j.Slf4j;
2929
import org.apache.poi.ss.usermodel.Workbook;
3030

3131
import java.io.IOException;
32+
import java.io.InputStream;
33+
import java.nio.file.Files;
3234
import java.nio.file.Path;
3335
import java.util.Arrays;
3436
import java.util.LinkedList;
@@ -66,6 +68,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
6668
* workbook
6769
*/
6870
private Workbook workbook;
71+
/**
72+
* 待追加excel
73+
*/
74+
private Path excel;
6975
/**
7076
* 文件分割,excel容量
7177
*/
@@ -91,10 +97,10 @@ public class DefaultStreamExcelBuilder<T> extends AbstractSimpleExcelBuilder imp
9197
*/
9298
private TemplateHandler templateHandler;
9399

94-
private List<CompletableFuture<Void>> asyncAppendFutures = new LinkedList<>();
100+
private final List<CompletableFuture<Void>> asyncAppendFutures = new LinkedList<>();
95101

96102
private DefaultStreamExcelBuilder(Class<T> dataType) {
97-
this(dataType, null);
103+
this(dataType, (Workbook) null);
98104
}
99105

100106
private DefaultStreamExcelBuilder(Class<T> dataType, Workbook workbook) {
@@ -105,14 +111,22 @@ private DefaultStreamExcelBuilder(Class<T> dataType, Workbook workbook) {
105111
this.isMapBuild = dataType == Map.class;
106112
}
107113

114+
private DefaultStreamExcelBuilder(Class<T> dataType, Path excel) {
115+
super(false);
116+
this.dataType = dataType;
117+
this.excel = excel;
118+
configuration.setWidthStrategy(WidthStrategy.NO_AUTO);
119+
this.isMapBuild = dataType == Map.class;
120+
}
121+
108122
/**
109123
* 获取实例,设定需要渲染的数据的类类型
110124
*
111125
* @param dataType 数据的类类型
112126
* @param <T> T
113127
* @return DefaultStreamExcelBuilder
114128
*/
115-
public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType) {
129+
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType) {
116130
return new DefaultStreamExcelBuilder<>(dataType);
117131
}
118132

@@ -124,10 +138,34 @@ public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType) {
124138
* @param <T> T
125139
* @return DefaultStreamExcelBuilder
126140
*/
127-
public static <T> DefaultStreamExcelBuilder<T> of(@NonNull Class<T> dataType, @NonNull Workbook workbook) {
141+
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, Workbook workbook) {
128142
return new DefaultStreamExcelBuilder<>(dataType, workbook);
129143
}
130144

145+
/**
146+
* 获取实例,设定需要渲染的数据的类类型
147+
*
148+
* @param dataType 数据的类类型
149+
* @param excel excel
150+
* @param <T> T
151+
* @return DefaultStreamExcelBuilder
152+
*/
153+
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, Path excel) {
154+
return new DefaultStreamExcelBuilder<>(dataType, excel);
155+
}
156+
157+
/**
158+
* 获取实例,设定需要渲染的数据的类类型
159+
*
160+
* @param dataType 数据的类类型
161+
* @param excelInputStream excelInputStream
162+
* @param <T> T
163+
* @return DefaultStreamExcelBuilder
164+
*/
165+
public static <T> DefaultStreamExcelBuilder<T> of(Class<T> dataType, InputStream excelInputStream) {
166+
return of(dataType, TempFileOperator.convertToFile(excelInputStream));
167+
}
168+
131169
/**
132170
* 已过时,请使用of方法代替
133171
* 4.0版本移除
@@ -151,22 +189,22 @@ public static DefaultStreamExcelBuilder<Map> getInstance(Workbook workbook) {
151189
return new DefaultStreamExcelBuilder<>(Map.class, workbook);
152190
}
153191

154-
public DefaultStreamExcelBuilder<T> titles(@NonNull List<String> titles) {
192+
public DefaultStreamExcelBuilder<T> titles(List<String> titles) {
155193
this.titles = titles;
156194
return this;
157195
}
158196

159-
public DefaultStreamExcelBuilder<T> sheetName(@NonNull String sheetName) {
197+
public DefaultStreamExcelBuilder<T> sheetName(String sheetName) {
160198
configuration.setSheetName(sheetName);
161199
return this;
162200
}
163201

164-
public DefaultStreamExcelBuilder<T> fieldDisplayOrder(@NonNull List<String> fieldDisplayOrder) {
202+
public DefaultStreamExcelBuilder<T> fieldDisplayOrder(List<String> fieldDisplayOrder) {
165203
this.fieldDisplayOrder = fieldDisplayOrder;
166204
return this;
167205
}
168206

169-
public DefaultStreamExcelBuilder<T> workbookType(@NonNull WorkbookType workbookType) {
207+
public DefaultStreamExcelBuilder<T> workbookType(WorkbookType workbookType) {
170208
if (workbook != null) {
171209
throw new IllegalArgumentException("Workbook type confirmed, not modifiable");
172210
}
@@ -184,13 +222,13 @@ public DefaultStreamExcelBuilder<T> noStyle() {
184222
return this;
185223
}
186224

187-
public DefaultStreamExcelBuilder<T> widthStrategy(@NonNull WidthStrategy widthStrategy) {
225+
public DefaultStreamExcelBuilder<T> widthStrategy(WidthStrategy widthStrategy) {
188226
configuration.setWidthStrategy(widthStrategy);
189227
return this;
190228
}
191229

192230
@Deprecated
193-
public DefaultStreamExcelBuilder<T> autoWidthStrategy(@NonNull AutoWidthStrategy autoWidthStrategy) {
231+
public DefaultStreamExcelBuilder<T> autoWidthStrategy(AutoWidthStrategy autoWidthStrategy) {
194232
configuration.setWidthStrategy(AutoWidthStrategy.map(autoWidthStrategy));
195233
return this;
196234
}
@@ -298,6 +336,15 @@ public DefaultStreamExcelBuilder<T> start() {
298336
if (head != null) {
299337
htmlToExcelStreamFactory.appendTitles(head);
300338
}
339+
if (excel != null && Files.exists(excel)) {
340+
log.info("start reading existing excel data.");
341+
SaxExcelReader<T> reader = SaxExcelReader.of(dataType)
342+
.readAllSheet();
343+
if (titleLevel > 0) {
344+
reader.rowFilter(row -> row.getRowNum() > titleLevel - 1);
345+
}
346+
reader.readThen(excel.toFile(), (Consumer<T>) this::append);
347+
}
301348
return this;
302349
}
303350

src/main/java/com/github/liaochong/myexcel/core/HtmlToExcelStreamFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,9 @@ public void cancel() {
398398
}
399399

400400
public void clear() {
401+
if (receiveThread != null && receiveThread.isAlive()) {
402+
receiveThread.interrupt();
403+
}
401404
closeWorkbook();
402405
TempFileOperator.deleteTempFiles(tempFilePaths);
403406
}

src/main/java/com/github/liaochong/myexcel/core/SaxExcelReader.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.github.liaochong.myexcel.core;
1616

1717
import com.github.liaochong.myexcel.core.cache.StringsCache;
18-
import com.github.liaochong.myexcel.core.constant.Constants;
1918
import com.github.liaochong.myexcel.exception.ExcelReadException;
2019
import com.github.liaochong.myexcel.exception.SaxReadException;
2120
import com.github.liaochong.myexcel.exception.StopReadException;
@@ -37,7 +36,6 @@
3736
import javax.xml.parsers.ParserConfigurationException;
3837
import java.io.File;
3938
import java.io.FileInputStream;
40-
import java.io.FileOutputStream;
4139
import java.io.IOException;
4240
import java.io.InputStream;
4341
import java.nio.file.Files;
@@ -177,29 +175,14 @@ public void readThen(File file, Function<T, Boolean> function) {
177175
}
178176

179177
private void doRead(InputStream fileInputStream) {
180-
Path path = this.convertToFile(fileInputStream);
178+
Path path = TempFileOperator.convertToFile(fileInputStream);
181179
try {
182180
doRead(path.toFile());
183181
} finally {
184182
TempFileOperator.deleteTempFile(path);
185183
}
186184
}
187185

188-
private Path convertToFile(InputStream is) {
189-
Path tempFile = TempFileOperator.createTempFile("i_t", Constants.XLSX);
190-
try (FileOutputStream fos = new FileOutputStream(tempFile.toFile())) {
191-
byte[] buffer = new byte[8 * 1024];
192-
int len;
193-
while ((len = is.read(buffer)) > 0) {
194-
fos.write(buffer, 0, len);
195-
}
196-
} catch (IOException e) {
197-
TempFileOperator.deleteTempFile(tempFile);
198-
throw new SaxReadException("Fail to convert file inputStream to temp file", e);
199-
}
200-
return tempFile;
201-
}
202-
203186
private void doRead(File file) {
204187
FileMagic fm;
205188
try (InputStream is = FileMagic.prepareToCheckMagic(new FileInputStream(file))) {

src/main/java/com/github/liaochong/myexcel/utils/TempFileOperator.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@
1717

1818

1919
import com.github.liaochong.myexcel.core.MyExcelConfiguration;
20+
import com.github.liaochong.myexcel.core.constant.Constants;
2021
import com.github.liaochong.myexcel.exception.ExcelBuildException;
22+
import com.github.liaochong.myexcel.exception.SaxReadException;
2123
import lombok.extern.slf4j.Slf4j;
2224

25+
import java.io.FileOutputStream;
2326
import java.io.IOException;
27+
import java.io.InputStream;
2428
import java.nio.file.Files;
2529
import java.nio.file.Path;
2630
import java.util.List;
@@ -106,4 +110,19 @@ public static void deleteTempFile(Path path) {
106110
}
107111
}
108112

113+
public static Path convertToFile(InputStream is) {
114+
Path tempFile = createTempFile("i_t", Constants.XLSX);
115+
try (FileOutputStream fos = new FileOutputStream(tempFile.toFile())) {
116+
byte[] buffer = new byte[8 * 1024];
117+
int len;
118+
while ((len = is.read(buffer)) > 0) {
119+
fos.write(buffer, 0, len);
120+
}
121+
} catch (IOException e) {
122+
TempFileOperator.deleteTempFile(tempFile);
123+
throw new SaxReadException("Fail to convert file inputStream to temp file", e);
124+
}
125+
return tempFile;
126+
}
127+
109128
}

src/test/java/com/github/liaochong/myexcel/core/DefaultStreamExcelBuilderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.File;
1616
import java.math.BigDecimal;
1717
import java.nio.file.Path;
18+
import java.nio.file.Paths;
1819
import java.time.LocalDate;
1920
import java.time.LocalDateTime;
2021
import java.util.ArrayList;
@@ -243,6 +244,17 @@ void appendTemplateBuild() throws Exception {
243244
}
244245
}
245246

247+
@Test
248+
void appendExistExcel() throws Exception {
249+
try (DefaultStreamExcelBuilder<CommonPeople> excelBuilder = DefaultStreamExcelBuilder.of(CommonPeople.class, Paths.get(TEST_OUTPUT_DIR + "common_build.xlsx"))
250+
.fixedTitles()
251+
.start()) {
252+
data(excelBuilder, 5000);
253+
Workbook workbook = excelBuilder.build();
254+
FileExportUtil.export(workbook, new File(TEST_OUTPUT_DIR + "common_build.xlsx"));
255+
}
256+
}
257+
246258
private void data(DefaultStreamExcelBuilder<CommonPeople> excelBuilder, int size) {
247259
BigDecimal oddMoney = new BigDecimal(109898);
248260
BigDecimal evenMoney = new BigDecimal(66666);

0 commit comments

Comments
 (0)