zoukankan      html  css  js  c++  java
  • Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的解决方法

    原本在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

  • 相关阅读:
    navigator对象及属性(userAgent)(扩展)
    最新Visual C++ 运行时
    Wakfu .pk 音频文件提取
    Flex布局学习记录
    小程序 swiper bindChange 抖动解决方法
    小程序 scroll-view 无法触发 onReachBottom 解决办法
    小程序修改按钮宽高
    db.collection(变量名)
    小程序图片轮播自适应
    微信小程序 MD5引用
  • 原文地址:https://www.cnblogs.com/codedreams/p/5662740.html
Copyright © 2011-2022 走看看