总结:ASP.Net网站拥有几百个用户 . 数据导出到Excel文件,这些文件可能相对较大(约5 MB) .
在试验阶段(只有少数用户),我们已经在导出方法中看到服务器上偶尔出现错误 .
这是堆栈跟踪:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. --->
System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at
System.IO.MemoryStream.set_Capacity(Int32 value) at
System.IO.MemoryStream.EnsureCapacity(Int32 value) at
System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Packaging.TrackingMemoryStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Packaging.SparseMemoryStream.WriteAndCollapseBlocks(Byte[] buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Packaging.SparseMemoryStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Packaging.CompressEmulationStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Packaging.CompressStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Zip.ProgressiveCrcCalculatingStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
MS.Internal.IO.Zip.ZipIOModeEnforcingStream.Write(Byte[]buffer, Int32 offset, Int32 count) at
System.IO.StreamWriter.Flush(BooleanflushStream, Boolean flushEncoder) at
System.IO.StreamWriter.Write(String value) at
System.Xml.XmlTextEncoder.Write(String text) at
System.Xml.XmlTextWriter.WriteString(String text) at
System.Xml.XmlText.WriteTo(XmlWriter w) at
System.Xml.XmlAttribute.WriteContentTo(XmlWriter w) at
System.Xml.XmlAttribute.WriteTo(XmlWriter w) at
System.Xml.XmlElement.WriteTo(XmlWriter w) at
System.Xml.XmlElement.WriteContentTo(XmlWriter w) at
System.Xml.XmlElement.WriteTo(XmlWriter w) at
System.Xml.XmlElement.WriteContentTo(XmlWriter w) at
System.Xml.XmlElement.WriteTo(XmlWriter w) at
System.Xml.XmlElement.WriteContentTo(XmlWriter w) at
System.Xml.XmlElement.WriteTo(XmlWriter w) at
System.Xml.XmlDocument.WriteContentTo(XmlWriter xw) at
System.Xml.XmlDocument.WriteTo(XmlWriter w) at
System.Xml.XmlDocument.Save(Stream outStream) at
OfficeOpenXml.ExcelWorksheet.Save() in
C:\temp\XXXXXXXXXX\ExcelPackage\ExcelWorksheet.cs:line 605 at
OfficeOpenXml.ExcelWorkbook.Save() in
C:\temp\XXXXXXXXXX\ExcelPackage\ExcelWorkbook.cs:line 439 at
OfficeOpenXml.ExcelPackage.Save() in
C:\temp\XXXXXXXXXX\ExcelPackage\ExcelPackage.cs:line 348 at
Framework.Exporting.Business.ExcelExport.BuildReport(HttpContext context) at
WebUserControl.BtnXLS_Click(Object sender, EventArgs e) in
C:\TEMP\XXXXXXXXXX\XXXXXXXXXX\XXXXXXX\UserControls\ExportToExcel.ascx.cs:line 108 at
System.Web.UI.WebControls.Button.OnClick(EventArgs e) at
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) at
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) at
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
---End of inner exception stack trace ---
at
System.Web.UI.Page.HandleError(Exception e) at
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at
System.Web.UI.Page.ProcessRequest() at
System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context) at
System.Web.UI.Page.ProcessRequest(HttpContext context) at
ASP.XXXXXXXXXXX_aspx.ProcessRequest(HttpContext context) in
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\TemporaryASP.NET
Files\XXXX\cdf32a52\d1a5eabd\App_Web_enxdwlks.1.cs:line 0 at
System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
即使不考虑这个特殊问题,通常导出到Excel需要在服务器上为每个请求实例化巨大的Excel对象,我一直认为这意味着取消Excel在高负载服务器上“严肃”工作的资格 . 是否有任何通用的方式以“轻量级”方式导出到Excel?将数据流式传输到CSV文件只是为此工作吗?
3 回答
如果格式不是问题,我会对CSV说“是” .
我注意到的另一件事是,你正在使用ExcelPackage . 我在使用大文件时遇到了一些问题 . 我认为这是处理xml的方式,导出大文件的速度非常慢 . 我建议使用其他一些库 .
我会把它卸到另一台机器上 . 创建一个Windows服务,该服务接受命令,生成文件,将其压缩,将其保存到磁盘,然后返回文件的路径,以便Web应用程序可以为其提供服务 . 这是您不希望陷入Web服务器的真正内存密集型操作之一 .
我知道这个帖子真的很旧但是如果你使用的是ExcelPackage,我在“LoadFromDataTable”调用中不断获得内存不足的异常,这个数据表有大约300k行 . 我找到了我的意思是大约100k,错误会偶尔发生 .
我在大约50k时使用了一个split datatable方法,并遍历了datatable集合,然后调用了“LoadFromDataTable”方法 . 似乎只是工作问题是方法删除前一个 . 如果我能弄清楚如何追加我会没事的 . 我告诉你,Excel出口最好 .