• 导出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();
            }

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

  • 相关阅读:
    眼过千遍不如手过一遍!
    等老了,做一个视频编辑
    不建议用wxWidgets,底层有过多的bug
    MFC新婚之夜(笑昏,大概是指MFC的人固步自封)
    Twitter算法
    Ruby on rails3
    重提基数排序
    Hashtable Dictionary List
    try { var mergeFilePath = string.Format("{0}mergepdf.pdf", tempDownDir); PDFPrintHelper.MergePDFFile(pdfList, mergeFi
    查找树
  • 原文地址:https://www.cnblogs.com/ares-yang/p/7212527.html
Copyright © 2020-2023  润新知