EPPlus为什么会引发System.ObjectDisposedException:无法访问封闭的Stream.写入MemoryStream并试图在ASP.NET MVC应用程序中定位零后?
public FileStreamResult ExportToExcel()
using (var memoryStream = new MemoryStream())
using (var excel = new ExcelPackage())
var worksheet = excel.Workbook.Worksheets.Add("Products");
worksheet.Cells["A1"].LoadFromCollection(Collection: myCollection, PrintHeaders: true);
excel.SaveAs(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin); // also tried memoryStream.Position = 0;
return new FileStreamResult(memoryStream, contentType)
FileDownloadName = "test.xlsx",
如果我将var memoryStream = …从using语句中移出,它可以工作,但感觉这可能会泄漏大量内存,导致较大的结果,直到下一次垃圾回收为止(如果未明确处置).也许ExcelPackage也处理MemoryStream吗?
全栈跟踪:
[ObjectDisposedException: Cannot access a closed Stream.]
System.IO.__Error.StreamIsClosed() +57
System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count) +10653946
System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) +80
System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +168
System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
System.Web.Mvc.Async.<>c__DisplayClass28.b__19() +173
System.Web.Mvc.Async.<>c__DisplayClass1e.b__1b(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39
System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.b__4(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9629296
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
解决方法:
我认为这是因为您的MemoryStream正在使用.
这导致(在处理结束时)流被关闭和处理.
让FileStreamResult处理此操作,并删除使用.
标签:memorystream,epplus,asp-net,c,asp-net-mvc
来源: https://codeday.me/bug/20191029/1963469.html
EPPlus为什么会引发System.ObjectDisposedException:无法访问封闭的Stream.写入MemoryStream并试图在ASP.NET MVC应用程序中定位零后?public FileStreamResult ExportToExcel(){using (var memoryStream = new MemoryStream())using (var excel = ...
背景介绍:基于netcore2.2开发api接口程序,自定义了一个异常捕获中间件,用于捕获未经处理的异常以及状态码404、500等访问(设计的出发点就是,出现了非200的响应,我这边全部会进行处理成200,并返回固定格式的JSON格式数据),并进行统一的信息返回。
返回的JSON实体定义如下:
最近都在参与公司的狐小E项目(https://www.hixiaoe.com/),一天到晚都是Mysql相关的知识,弄多了难免乏味,正好其他兄弟团队碰到了一个
C#
用NPOI生成Excel模板的小问题,帮助解决了一下,换一下脑子还是不错的。
兄弟团队是想用NPOI生成一个下载模板,让用户能把在系统里配置好的模板模板,生成Excel下载下来,并且在填Excel的时候有一些限制,比如产品分类,只能选定固定一些值,这些值是系统里提前配置好的。如下图。
原本在CS项目中用的好好的在BS项目中既然提示我导出出现
无法访问
已关闭的
流
的解决方法 比较郁闷经过研究 终于解决了先将方法发出来 让遇到此问题的筒子们以作参考
//新建类 重写Npoi
流
方法
public class NpoiMemory
Stream
: Memory
Stream
public NpoiMemory
Stream
()
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
<input type="file" name="file" />
<input type="submit" value="Upload" />
在控制器中添加一个接收文件的方法:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
if (file != null && file.ContentLength > 0)
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
return RedirectToAction("In
dex
");
return View();
在上面的示例中,我们首先检查文件是否为空并且大小是否大于零。如果文件不为空,我们将文件名和
保存
路径组合起来并将文件
保存
到服务器上。最后,我们重定向到 In
dex
视图。
希望这可以帮助你解决问题。如果你有任何进一步的问题,请随时问我。