c# – 导出到excel抛出System.OutOfMemoryException
发布时间:2020-12-16 01:57:31 所属栏目:百科 来源:网络整理
导读:尝试将数据表从内存流写入输出流时的网页在尝试将excel文件传递给用户时抛出System.OutOfMemoryException.我使用Closed XML在Excel中保存文件,数据表大约有40K行和150列大,主要包含小数,导出的文件通常是10MB或更大.在导出到excel时,有哪些建议的技巧可以解
尝试将数据表从内存流写入输出流时的网页在尝试将excel文件传递给用户时抛出System.OutOfMemoryException.我使用Closed
XML在Excel中保存文件,数据表大约有40K行和150列大,主要包含小数,导出的文件通常是10MB或更大.在导出到excel时,有哪些建议的技巧可以解决大数据集?
这是我使用的封闭式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; } 解决方法
我偶然发现了这个链接
OpenXML libraries (alternatives to ClosedXML)当大量数据导出到Excel时,EPPlus
https://epplus.codeplex.com/比ClosedXML工作得更好. 至少没有“OutOfMemory”例外,因为EPPlus似乎避开了Memory Streams,尽管我仍然有兴趣了解他们是如何实现这一点的,甚至是了解Closed XML和EPPlus之间的区别.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |