海豚宝宝的代码生活

专注于.Net系统开发

导航

把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"

posted on 2007-08-23 14:07  Bruse  阅读(185)  评论(0)    收藏  举报