 /// <summary>
    /// <summary> /// 从gridview控件导出Excel
    /// 从gridview控件导出Excel /// </summary>
    /// </summary> /// <param name="ctl"></param>
    /// <param name="ctl"></param> public void ToExcel(System.Web.UI.Control ctl)
    public void ToExcel(System.Web.UI.Control ctl) {
    { HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls"); HttpContext.Current.Response.Charset = "UTF-8";
        HttpContext.Current.Response.Charset = "UTF-8"; HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
        HttpContext.Current.Response.ContentType = "application/ms-excel";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword  ctl.Page.EnableViewState = false;
        ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter();
        System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
        System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw);
        ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End();
        HttpContext.Current.Response.End(); }
    }  /// <summary>
    /// <summary> /// 空的VerifyRenderingInServerForm方法(必须写,防止报错),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件。
    /// 空的VerifyRenderingInServerForm方法(必须写,防止报错),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件。 /// </summary>
    /// </summary> /// <param name="control"></param>
   /// <param name="control"></param> public override void VerifyRenderingInServerForm(Control control)
    public override void VerifyRenderingInServerForm(Control control) {
    { //base.VerifyRenderingInServerForm(control);
        //base.VerifyRenderingInServerForm(control);    }
    }
 注意:如果导出按纽在gridview中,就需要在aspx文件中的<%@ Page
注意:如果导出按纽在gridview中,就需要在aspx文件中的<%@ Page >里加入EnableEventValidation="false"
>里加入EnableEventValidation="false"  
                     
                    
                 
                    
                 

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号