I want to generate big xlsx file, but don't want to keep it in memory of the server and get OutOfMemoryException
.
So I read the data from database page by page, generate rows with OpenXmlWriter and send it part by part to the client:
// I use `MemoryStream` as the buffer
OpenXmlWriter = OpenXmlWriter.Create(OutputStream);
OpenXmlWriter.WriteStartElement(new Worksheet());
OpenXmlWriter.WriteStartElement(new SheetData());
foreach(var row in rows)
{
//...write cells with OpenXmlWriter and then
OutputStream.Position = 0;
var buffer = new byte[OutputStream.Length];
OutputStream.Read(buffer, 0, (int)OutputStream.Length);
FlushCalback(buffer);
OutputStream.SetLength(0);
System.Web.HttpContext.Current.Response.BinaryWrite(dataBuffer);
}
But after downloading I found, that it generates only xml markup, while xlsx is actualy "package". I can't find any example how to do that. Is there any solution? Or another libraries ?
UPDATE:
SpreadsheetDocument could help, but it writes ALL the data to the stream after calling Save()
method. And it will rewrite everithing after each call.
SpreadsheetDocument = SpreadsheetDocument.Create(OutputStream, SpreadsheetDocumentType.Workbook);
//... generate rows
SpreadsheetDocument.Save();