Web gridview 导出Excel

Response.Clear();
Response.Buffer=true;
Response.Charset="GB2312";//
Response.AddHeader("Content-Disposition","attachment;filename=filename.xls");
Response.ContentEncoding=Encoding.UTF8;
Response.ContentType="Application/ms-excel";
System.IO.StringWriter writer=new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlwriter=new System.Web.UI.HtmlTextWriter(writer);
GridView1.RenderToControl(htmlwriter);
Response.output.write(writer.ToString());
Response.Flush();
Response.End();

1.还要页面上加一个 EnableEventValidation="false"
2.重载一个方法
    public override void VerifyRenderingInServerForm(Control control)
    {
    }


posted on 2009-07-30 11:10  爱笑的猪  阅读(319)  评论(3编辑  收藏  举报

导航