把GridView中的信息直接导出到Excel

// 通过按钮发起输出动作
protected   void  BT_ExportReport_Click( object  sender, EventArgs e)
    
{
        System.Web.HttpContext.Current.Response.ClearContent();
        System.Web.HttpContext.Current.Response.AddHeader(
"content-disposition""attachment; filename=excel.xls");
        System.Web.HttpContext.Current.Response.ContentType 
= "application/excel";

        System.IO.StringWriter sw 
= new System.IO.StringWriter();
        HtmlTextWriter htw 
= new HtmlTextWriter(sw);

        
// 将服务器控件的内容输出到所提供的 System.Web.UI.HtmlTextWriter 对象中
        GridView1.RenderControl(htw);

        System.Web.HttpContext.Current.Response.Write(sw.ToString());
        System.Web.HttpContext.Current.Response.End();
    }


    
public   override   void  VerifyRenderingInServerForm(Control control)
    
{

    }
与此同时,要在页面端的<@Page>中加入 EnableEventValidation="false"

你可能感兴趣的:(GridView)