Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流
生活随笔
收集整理的這篇文章主要介紹了
Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
NPOI生產(chǎn).xlsx文件件時(shí),在使用book.Write(ms);后,會(huì)關(guān)閉流,這樣導(dǎo)致再次使用Respons輸出流的時(shí)候就出錯(cuò)了。
造成關(guān)閉流的主要原因有時(shí)其實(shí)是跨域,同域是沒有問題的。
//新建類 重寫Npoi流方法 public class NpoiMemoryStream : MemoryStream{public NpoiMemoryStream(){AllowClose = true;}public bool AllowClose { get; set; }public override void Close(){if (AllowClose)base.Close();} } //導(dǎo)出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();原文:https://blog.csdn.net/eit520/article/details/53231642?
?
轉(zhuǎn)載于:https://www.cnblogs.com/shy1766IT/p/11074548.html
總結(jié)
以上是生活随笔為你收集整理的Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 建行大山白全称是什么?申请条件有哪些?
- 下一篇: 建行大山白年费多少?可以免年费吗?