I have seen many posts about this issue but no working solutions. I have a web application which writes XLS files using HSSFWorkbook through MemoryStream just fine. I want to have my application export files as XLSX now. I changed all HSSF objects to XSSF figuring that would work. I got the following error:
"An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but was not handled in user code. Additional information: Cannot access a closed Stream."
Apparently the XSSF Write method is designed to close the stream once it is written by default. To me this makes absolutely zero sense since if it closes immediately you can never do anything with the object you just attempted to write. Does anyone have an alternative way to write XLSX files to a MemoryStream for download from a web application or should I ditch NPOI for another library?
NOTE: I do not want FileStream examples. We do not want to write temporary files to disk in order to download.
"An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but was not handled in user code. Additional information: Cannot access a closed Stream."
Apparently the XSSF Write method is designed to close the stream once it is written by default. To me this makes absolutely zero sense since if it closes immediately you can never do anything with the object you just attempted to write. Does anyone have an alternative way to write XLSX files to a MemoryStream for download from a web application or should I ditch NPOI for another library?
NOTE: I do not want FileStream examples. We do not want to write temporary files to disk in order to download.
context.Response.Clear();
context.Response.ContentType = Helper.ContentType(".xlsx");
context.Response.AddHeader("Content-Disposition", string.Format("inline;filename={0}.xlsx;", this.FileName));
using (MemoryStream MyMemory = new MemoryStream())
{
MyWorkbook.Write(MyMemory);
context.Response.AddHeader("Content-Length", MyMemory.Length.ToString());
context.Response.BinaryWrite(MyMemory.GetBuffer());
}
context.Response.End();