问题描述
我收到 SocketConnectionException
同时生成Excel文件进行批量数据(超过0.5万条记录)。
我的Web应用程序的code写入`的OutputStream。这里是code的一个片段:
而(sr.next()){
反++; //提前计数器
鉴于=(数据类)sr.get(0);
尝试{
//为活动报告文件写入字段的值
reportService.writeExcelFieldsValue(rowCounter,表,视图,用户,exportedFields);
rowCounter ++;
}赶上(例外五){
抛出新的RuntimeException(E);
} 如果(反== || CHUNKSIZE sr.isLast()){
计数器= 0; //计数器复位
//清除会话一大块后下一大块之前
的getSession()清()。
}
}
wb.write(BOS);
bos.flush();
POI提供了内置的XSSF之上的低内存占用SXSSF API。
SXSSF XSSF是一个API兼容的流媒体扩展时非常大S preadsheets必须生产到使用,堆空间是有限的。 SXSSF通过限制访问是一个滑动窗口内的行实现其低内存占用,而XSSF给出文档中访问到的所有行。旧版的行是在窗口不再变得不可访问,因为它们被写入磁盘。
在可指定的访问窗口的大小自动刷新模式中,保持在存储器中的特定行数的。当达到该值时,附加的行的创建导致待从接入窗取出并写入磁盘具有最低索引的行。或者,窗口的大小可以被设置为动态地增长;它可以通过周期性的显式调用进行修整根据需要flushRows(INT keepRows)。
由于执行的流传输性质,有以下限制时相比XSSF:
- 只有行的有限数量的在一个时间点进行访问。
- 不支持Sheet.clone()。不支持公式求值
- 我觉得这个链接可以帮助你
我想你会想使用XSSF EventModel code。请参阅上手。或了解更多详情,请点击
另外这个链接可以帮助你。
Writing一个大的结果集到Excel文件使用POI
I am getting SocketConnectionException
while generating Excel file for bulk data (more than 0.5 million records).
The code of my web application writes to `outputstream. Here's a snippet of code:
while (sr.next()) {
counter++; //advance counter
view = (DataClass) sr.get(0);
try {
//writing fields values for Activity Report file
reportService.writeExcelFieldsValue(rowCounter,sheet,view,user,exportedFields);
rowCounter++;
} catch (Exception e) {
throw new RuntimeException(e);
}
if (counter == chunkSize || sr.isLast()) {
counter = 0; //reset counter
//Clear the session after a chunk and before next chunk
getSession().clear();
}
}
wb.write(bos);
bos.flush();
POI provides a low-memory footprint SXSSF API built on top of XSSF.
SXSSF is an API-compatible streaming extension of XSSF to be used when very large spreadsheets have to be produced, and heap space is limited. SXSSF achieves its low memory footprint by limiting access to the rows that are within a sliding window, while XSSF gives access to all rows in the document. Older rows that are no longer in the window become inaccessible, as they are written to the disk.
In auto-flush mode the size of the access window can be specified, to hold a certain number of rows in memory. When that value is reached, the creation of an additional row causes the row with the lowest index to to be removed from the access window and written to disk. Or, the window size can be set to grow dynamically; it can be trimmed periodically by an explicit call to flushRows(int keepRows) as needed.
Due to the streaming nature of the implementation, there are the following limitations when compared to XSSF:
- Only a limited number of rows are accessible at a point in time.
- Sheet.clone() is not supported. Formula evaluation is not supported
- I think this link might help you
I think you'll want to use the XSSF EventModel code. See the POI documentation to get started. or For more details, click here
Also this link may help you.Writing a large resultset to an Excel file using POI
这篇关于插座异常而genrating批量使用Apache POI生成Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!