Skip to content

Commit

Permalink
🐛 fixes #37
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Jul 27, 2020
1 parent b801132 commit e37d04b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.OutputStream;
import java.lang.reflect.Field;
Expand All @@ -42,12 +43,12 @@
@Slf4j
public abstract class ExcelWriter {

private int rowNum;
private Sheet sheet;
private int rowNum;
private Sheet sheet;
private Map<Integer, Field> fieldIndexes;
private List<ExcelColumn> columns;
private List<ExcelColumn> columns;

Workbook workbook;
Workbook workbook;
OutputStream outputStream;

ExcelWriter(OutputStream outputStream) {
Expand Down Expand Up @@ -76,7 +77,7 @@ void writeSheet(Writer writer) throws WriterException {
// setting styles
CellStyle headerStyle = null;
CellStyle columnStyle = null;
CellStyle titleStyle = null;
CellStyle titleStyle = null;
if (null != writer.headerStyle()) {
headerStyle = writer.headerStyle().accept(workbook, workbook.createCellStyle());
}
Expand All @@ -91,8 +92,8 @@ void writeSheet(Writer writer) throws WriterException {
writer.sheetConsumer().accept(sheet);
} else {
// compute the Filed to be written
Collection<?> rows = writer.rows();
Field[] fields = rows.iterator().next().getClass().getDeclaredFields();
Collection<?> rows = writer.rows();
Field[] fields = rows.iterator().next().getClass().getDeclaredFields();

this.fieldIndexes = new HashMap<>(fields.length);
this.columns = new ArrayList<>();
Expand Down Expand Up @@ -140,6 +141,9 @@ void writeSheet(Writer writer) throws WriterException {
// write to OutputStream
try (OutputStream os = outputStream) {
workbook.write(os);
if (workbook instanceof SXSSFWorkbook) {
((SXSSFWorkbook) workbook).dispose();
}
} catch (Exception e) {
throw new WriterException("workbook write to OutputStream error", e);
}
Expand Down

0 comments on commit e37d04b

Please sign in to comment.