原本在CS項目中用的好好的在BS項目中既然提示我導出出現無法訪問已關閉的流的解決方法 比較郁悶經過研究 終於解決了先將方法發出來 讓遇到此問題的筒子們以作參考
//新建類 重寫Npoi流方法 public class NpoiMemoryStream : MemoryStream { public NpoiMemoryStream() { AllowClose = true; } public bool AllowClose { get; set; } public override void Close() { if (AllowClose) base.Close(); } }
導出Excel方法
//導出Excel文件的方法 var ms = new NpoiMemoryStream(); ms.AllowClose = false; workbook.Write(ms); ms.Flush(); ms.Seek(0, SeekOrigin.Begin); ms.AllowClose = true; HttpContext curContext = HttpContext.Current; curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = Encoding.UTF8; curContext.Response.Charset = ""; curContext.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8)); long fileSize = ms.Length; curContext.Response.AddHeader("Content-Length", fileSize.ToString()); curContext.Response.BinaryWrite(ms.GetBuffer()); curContext.Response.End();
如果您覺得此方法解決了您的疑惑請為點個推薦,讓此貼搜索命中率高一些讓遇到此問題的筒子們能盡快找到!Thanks