ASP.NET 导出到 Excel

时间:2022-12-16 14:45:04
try
    {
     Response.Clear();
     Response.Buffer= true;
     Response.Charset="gb2312"; 
     Response.AppendHeader("Content-Disposition","attachment;filename=FileFlow.xls");
          Response.ContentEncoding=System.Text.Encoding.GetEncoding("gb2312"); 
     Response.ContentType = "application/ms-Excel";
          this.EnableViewState = false;  
     System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
          System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
     this.dtgmark.RenderControl(oHtmlTextWriter);   
          //this 表示输出本页,你也可以绑定datagrid,或其他支持obj.RenderControl()属性的控件
          Response.Write(oStringWriter.ToString());
          Response.End(); 
    }
    catch
    {}