zoukankan      html  css  js  c++  java
  • 导出Excel 2007 (NPOI)

    今天在导出Excel2007时报了个错,问是否修复,点yes就提示修复正常了,但具体什么原因没说,如图

    之前简单的导出代码是这样写的

           public static void ExportToWeb(string strFileName,IWorkbook Workbook )
            {
                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(strFileName, Encoding.UTF8));
                var stream = WriteToStream(Workbook);
                curContext.Response.AddHeader("Content-Length", stream.ToArray().Length.ToString());
                curContext.Response.BinaryWrite(stream.GetBuffer());
                curContext.Response.End();
    }

    在2003上完全行的通,网上很多小伙伴07也是这样写的,但我打开时报错。

    后面这样改就可以了

            //导出流
            public static void ExportToWeb(string strFileName,IWorkbook Workbook )
            {
                HttpContext curContext = HttpContext.Current;
                curContext.Response.Charset = "UTF8";
                curContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
                strFileName = System.Web.HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8);
                // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 
                curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName);
                // 指定返回的是一个不能被客户端读取的流,必须被下载 
                curContext.Response.ContentType = "application/ms-Excel";
                // 把文件流发送到客户端 
                Workbook.Write(curContext.Response.OutputStream);
                // 停止页面的执行 
                curContext.Response.End();
            }

    做个记录,也希望帮到遇到同样问题的伙伴。

  • 相关阅读:
    基贝叶斯后验概率的拼写检查器实现 python
    Hadoop The Definitive Guide 2nd Edition 笔记
    DGV删除当前选中行
    DGV设置单元不可编辑
    DGV获取当前选中单元格
    HDU2112_HDU Today_有地名的最短路_map+SPFA
    HDU1026_优先队列+广搜+打印路径
    HDU2757_Ocean Currents_优先队列+广搜_入门题_十四周模拟赛
    自己写的SPFA模板(可打印路径)
    HDU2782_暴力深搜_第十四周模拟赛
  • 原文地址:https://www.cnblogs.com/ares-yang/p/7212527.html
Copyright © 2011-2022 走看看