nissl-lab / npoi

a .NET library that can read/write Office formats without Microsoft Office installed. No COM+, no interop.
Apache License 2.0
5.67k stars 1.43k forks source link

SXSSFWorkbook leaveOpen doesn't work as intend. #1411

Open jarukitppg opened 2 weeks ago

jarukitppg commented 2 weeks ago

NPOI Version

2.7.1

File Type

Issue Description

I have a large set of data, and I'm using it like this

SXSSFWorkbook workbook = new();
var dataSheet = workbook.CreateSheet("Data");

foreach(var dataRow in dataRows)
{
    WriteRow(dataRow, dataSheet);

    var buffer = new MemoryStream();
    workbook.Write(buffer, leaveOpen: true);

    buffer.Flush();
    buffer.Seek(0, SeekOrigin.Begin);
}

The first data row can be write perfectly fine, but the second loop, it splitting out an exception

Here is the exception I got.

"ClassName":"System.ObjectDisposedException","Message":"Cannot write to a closed TextWriter.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at System.IO.StreamWriter.g__ThrowObjectDisposedException|77_0()\r\n at System.IO.StreamWriter.Write(String value)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.WriteAsBytes(String text)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.BeginRow(Int32 rownum, SXSSFRow row)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.WriteRow(Int32 rownum, SXSSFRow row)\r\n at NPOI.XSSF.Streaming.SXSSFSheet.flushOneRow()\r\n at NPOI.XSSF.Streaming.SXSSFSheet.FlushRows(Int32 remaining, Boolean flushOnDisk)\r\n at NPOI.XSSF.Streaming.SXSSFSheet.FlushRows()\r\n at NPOI.XSSF.Streaming.SXSSFWorkbook.FlushSheets()\r\n at NPOI.XSSF.Streaming.SXSSFWorkbook.Write(Stream stream, Boolean leaveOpen)\r\n at ... "RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146232798,"Source":"System.Private.CoreLib","WatsonBuckets":null,"ObjectName":"StreamWriter"}

Also, I tried to use NPOI version 2.6.2, I got another exception instead of the above, here is the exception I got.

{"ClassName":"System.ObjectDisposedException","Message":"Cannot access a closed Stream.","Data":null,"InnerException":null,"HelpURL":null,"StackTraceString":" at System.IO.Strategies.BufferedFileStreamStrategy.Write(ReadOnlySpan`1 buffer)\r\n at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)\r\n at System.IO.StreamWriter.Write(String value)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.WriteAsBytes(String text)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.WriteCell(Int32 columnIndex, ICell cell)\r\n at NPOI.XSSF.Streaming.SheetDataWriter.WriteRow(Int32 rownum, SXSSFRow row)\r\n at NPOI.XSSF.Streaming.SXSSFSheet.flushOneRow()\r\n at NPOI.XSSF.Streaming.SXSSFSheet.FlushRows(Int32 remaining, Boolean flushOnDisk)\r\n at NPOI.XSSF.Streaming.SXSSFSheet.FlushRows()\r\n at NPOI.XSSF.Streaming.SXSSFWorkbook.FlushSheets()\r\n at NPOI.XSSF.Streaming.SXSSFWorkbook.Write(Stream stream, Boolean leaveOpen)\r\n at ... "RemoteStackTraceString":null,"RemoteStackIndex":0,"ExceptionMethod":null,"HResult":-2146232798,"Source":"System.Private.CoreLib","WatsonBuckets":null,"ObjectName":""}

Or did I do something wrong here?