diff --git a/src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java b/src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java index c329238..ffb011b 100644 --- a/src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java +++ b/src/main/java/io/github/biezhi/excel/plus/writer/ExcelWriter.java @@ -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; @@ -42,12 +43,12 @@ @Slf4j public abstract class ExcelWriter { - private int rowNum; - private Sheet sheet; + private int rowNum; + private Sheet sheet; private Map fieldIndexes; - private List columns; + private List columns; - Workbook workbook; + Workbook workbook; OutputStream outputStream; ExcelWriter(OutputStream outputStream) { @@ -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()); } @@ -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<>(); @@ -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); }