/// <summary>
    
/// 从gridview控件导出Excel
    
/// </summary>
    
/// <param name="ctl"></param>

    public void ToExcel(System.Web.UI.Control ctl)
    
{
        HttpContext.Current.Response.AppendHeader(
"Content-Disposition""attachment;filename=Excel.xls");
        HttpContext.Current.Response.Charset 
= "UTF-8";
        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 
        ctl.Page.EnableViewState = false;
        System.IO.StringWriter tw 
= new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter hw 
= new System.Web.UI.HtmlTextWriter(tw);
        ctl.RenderControl(hw);
        HttpContext.Current.Response.Write(tw.ToString());
        HttpContext.Current.Response.End();
    }
 
    
/// <summary>
    
/// 空的VerifyRenderingInServerForm方法(必须写,防止报错),以确认在运行时为指定的ASP.NET 服务器控件呈现HtmlForm 控件。
    
/// </summary>
   
/// <param name="control"></param>

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


注意:如果导出按纽在gridview中,就需要在aspx文件中的
<%@ Page>里加入EnableEventValidation="false" 
posted on 2007-07-11 17:16  阳春  阅读(192)  评论(0)    收藏  举报