一份网页在尝试将数据表从内存流写入输出流时,抛出了System.OutOfMemoryException异常,而我正在使用Closed XML将文件保存为Excel格式,数据表大约有40K行和150列,主要包含小数,导出的文件通常大于10MB。在导出到Excel时,有什么建议的技巧可以解决大型数据集的问题吗?这是我使用的来自Closed XML的代码: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;
}