A webpage when trying to write datatable from memory stream to output stream is throwing System.OutOfMemoryException while trying to deliver excel file to user. I'm using Closed XML for saving file in Excel and the datatable is about 40K rows and 150 columns large containing mostly decimals, exported file is usually 10MB or greater. What are the suggested tricks to hack around large data sets while exporting to excel?
This is the code from closed XML that I am using http://closedxml.codeplex.com/wikipage?title=How%20do%20I%20deliver%20an%20Excel%20file%20in%20ASP.NET%3f&referringTitle=Documentation
public HttpResponseMessage Get()
{
// Create the workbook
var workbook = new XLWorkbook();
Datatable dt = getDataTable();
workbook.Worksheets.Add(dt);
// Prepare the response
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
var memoryStream = new MemoryStream(); // If I put this in a 'using' construct, I never get the response back in a browser.
workbook.SaveAs(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin); // Seem to have to manually rewind stream before applying it to the content.
response.Content = new StreamContent(memoryStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "HelloWorld.xlsx" };
return response;
}