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

    NPOI生产.xlsx文件件时,在使用book.Write(ms);后,会关闭流,这样导致再次使用Respons输出流的时候就出错了。

    造成关闭流的主要原因有时其实是跨域,同域是没有问题的。

    //新建类 重写Npoi流方法
    public class NpoiMemoryStream : MemoryStream
        {
            public NpoiMemoryStream()
            {
                AllowClose = true;
            }
     
            public bool AllowClose { get; set; }
     
            public override void Close()
            {
                if (AllowClose)
                    base.Close();
            }
    }
    //导出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 

  • 相关阅读:
    spark学习进度18(SparkSQL读写)
    查看及修改centos的系统时间
    第3章 串
    linux就该这么学 简介
    1 快速入门
    rocketMQ实战与原理解析 简介
    绪论
    数据结构java版 第4版 叶核亚著 简介
    数据结构java语言版 简介
    数据结构与问题求解java版 简介
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/11074548.html
Copyright © 2011-2022 走看看