mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
fix Big writer bug
This commit is contained in:
parent
411fea9c9f
commit
cf19fe3003
@ -16,7 +16,8 @@
|
|||||||
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
* 【json 】 修复append方法导致的JSONConfig传递失效问题(issue#906@Github)
|
||||||
* 【core 】 修复CollUtil.subtractToList判断错误(pr#915@Github)
|
* 【core 】 修复CollUtil.subtractToList判断错误(pr#915@Github)
|
||||||
* 【poi 】 修复WordWriter写表格问题(pr#914@Github)
|
* 【poi 】 修复WordWriter写表格问题(pr#914@Github)
|
||||||
* 【core 】 修复IoUtil.readBytes缓存数组长度问题(issue#I1KIUE@Github)
|
* 【core 】 修复IoUtil.readBytes缓存数组长度问题(issue#I1KIUE@Gitee)
|
||||||
|
* 【core 】 修复BigExcelWriter多次flush导致的问题(issue#920@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
package cn.hutool.poi.excel;
|
package cn.hutool.poi.excel;
|
||||||
|
|
||||||
import java.io.File;
|
import cn.hutool.core.io.FileUtil;
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
|
||||||
|
|
||||||
import cn.hutool.core.io.FileUtil;
|
import java.io.File;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 大数据量Excel写出
|
* 大数据量Excel写出
|
||||||
@ -17,6 +18,11 @@ public class BigExcelWriter extends ExcelWriter {
|
|||||||
|
|
||||||
public static final int DEFAULT_WINDOW_SIZE = SXSSFWorkbook.DEFAULT_WINDOW_SIZE;
|
public static final int DEFAULT_WINDOW_SIZE = SXSSFWorkbook.DEFAULT_WINDOW_SIZE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* BigExcelWriter只能flush一次,因此调用后不再重复写出
|
||||||
|
*/
|
||||||
|
private boolean isFlushed;
|
||||||
|
|
||||||
// -------------------------------------------------------------------------- Constructor start
|
// -------------------------------------------------------------------------- Constructor start
|
||||||
/**
|
/**
|
||||||
* 构造,默认生成xls格式的Excel文件<br>
|
* 构造,默认生成xls格式的Excel文件<br>
|
||||||
@ -116,11 +122,22 @@ public class BigExcelWriter extends ExcelWriter {
|
|||||||
|
|
||||||
// -------------------------------------------------------------------------- Constructor end
|
// -------------------------------------------------------------------------- Constructor end
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ExcelWriter flush(OutputStream out, boolean isCloseOut) throws IORuntimeException {
|
||||||
|
if(false == isFlushed){
|
||||||
|
isFlushed = true;
|
||||||
|
return super.flush(out, isCloseOut);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
if (null != this.destFile) {
|
if (null != this.destFile && false == isFlushed) {
|
||||||
flush();
|
flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 清理临时文件
|
||||||
((SXSSFWorkbook) this.workbook).dispose();
|
((SXSSFWorkbook) this.workbook).dispose();
|
||||||
super.closeWithoutFlush();
|
super.closeWithoutFlush();
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,5 @@
|
|||||||
package cn.hutool.poi.excel.test;
|
package cn.hutool.poi.excel.test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.apache.poi.ss.usermodel.CellStyle;
|
|
||||||
import org.apache.poi.ss.usermodel.FillPatternType;
|
|
||||||
import org.apache.poi.ss.usermodel.Font;
|
|
||||||
import org.apache.poi.ss.usermodel.IndexedColors;
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.date.DateUtil;
|
import cn.hutool.core.date.DateUtil;
|
||||||
import cn.hutool.core.io.FileUtil;
|
import cn.hutool.core.io.FileUtil;
|
||||||
@ -19,7 +7,20 @@ import cn.hutool.core.map.MapUtil;
|
|||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
import cn.hutool.poi.excel.BigExcelWriter;
|
import cn.hutool.poi.excel.BigExcelWriter;
|
||||||
import cn.hutool.poi.excel.ExcelUtil;
|
import cn.hutool.poi.excel.ExcelUtil;
|
||||||
|
import cn.hutool.poi.excel.ExcelWriter;
|
||||||
import cn.hutool.poi.excel.style.StyleUtil;
|
import cn.hutool.poi.excel.style.StyleUtil;
|
||||||
|
import org.apache.poi.ss.usermodel.CellStyle;
|
||||||
|
import org.apache.poi.ss.usermodel.FillPatternType;
|
||||||
|
import org.apache.poi.ss.usermodel.Font;
|
||||||
|
import org.apache.poi.ss.usermodel.IndexedColors;
|
||||||
|
import org.junit.Ignore;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 写出Excel单元测试
|
* 写出Excel单元测试
|
||||||
@ -197,10 +198,23 @@ public class BigExcelWriteTest {
|
|||||||
@Test
|
@Test
|
||||||
@Ignore
|
@Ignore
|
||||||
public void writeCellValueTest() {
|
public void writeCellValueTest() {
|
||||||
String path = "e:/cellValueTest.xlsx";
|
String path = "d:/test/cellValueTest.xlsx";
|
||||||
FileUtil.del(path);
|
FileUtil.del(path);
|
||||||
BigExcelWriter writer = new BigExcelWriter(path);
|
BigExcelWriter writer = new BigExcelWriter(path);
|
||||||
writer.writeCellValue(3, 5, "aaa");
|
writer.writeCellValue(3, 5, "aaa");
|
||||||
writer.close();
|
writer.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@Ignore
|
||||||
|
public void closeTest() {
|
||||||
|
final Map<String, ?> map1 = MapUtil.of("id", "123456");
|
||||||
|
final Map<String, ?> map2 = MapUtil.of("id", "123457");
|
||||||
|
final List<?> data = Arrays.asList(map1, map2);
|
||||||
|
final String destFilePath = "d:/test/closeTest.xlsx";//略
|
||||||
|
FileUtil.del(destFilePath);
|
||||||
|
try (ExcelWriter writer = ExcelUtil.getBigWriter(destFilePath)) {
|
||||||
|
writer.write(data).flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user