zoukankan      html  css  js  c++  java
  • ASP.NET导出Excel文件

    //导出按钮事件

    protected void btnExport_Click(object sender, EventArgs e)
    {

      Response.Clear();

      Response.Buffer = true;
      Response.Charset = "GB2312";
      Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("文件名称.xls"));
      Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
      Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
      System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
      this.gvList.RenderControl(oHtmlTextWriter);//gvList为页面网格控件
      Response.Output.Write(oStringWriter.ToString());
      Response.Flush();
      Response.End();

     }
    注意:
    若执行出现错误为“异常信息:System.Web.HttpException: 类型“GridView”的控件“gvList”必须放在具有 runat=server 的窗体标记内。”
    在.cs文件中,添加以下方法即可解决

    public override void VerifyRenderingInServerForm(Control control)
    {
    //base.VerifyRenderingInServerForm(control);
    }

     
     
     
  • 相关阅读:
    10.16作业
    day0402作业
    day04作业
    10.13作业
    JVM原理最全、清晰、通俗讲解
    Java的SimpleDateFormat,DateTimeFormatter:YYYY与yyyy
    从源码层面理解 ArrayList 扩容策略
    哈夫曼树
    面向对象三大基本特性,五大基本原则
    数据结构
  • 原文地址:https://www.cnblogs.com/xiaohong520789/p/8484076.html
Copyright © 2011-2022 走看看