GridView导出到Excel

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.ClearContent();
 
        Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
 
        Response.ContentType = "application/excel";

        StringWriter sw = new StringWriter();

        HtmlTextWriter htw = new HtmlTextWriter(sw);
        this.GridView1.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
    如果没有下面方法会报错类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内
    public override void VerifyRenderingInServerForm(Control control)
    {

    }

posted @ 2008-02-07 12:32  一个IT愤青  阅读(155)  评论(0编辑  收藏  举报