1protected void Button1_Click(object sender, EventArgs e)
2 {
3 Response.Clear();
4 Response.Buffer = true;
5 Response.Charset = "GB2312";
6 Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
7 // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
8 Response.ContentEncoding = System.Text.Encoding.UTF8;
9 Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
10 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
11 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
12 gvProduct.RenderControl(oHtmlTextWriter);
13 Response.Output.Write(oStringWriter.ToString());
14 Response.Flush();
15 Response.End();
16 }
17 //确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
18 public override void VerifyRenderingInServerForm(Control control)
19 { }
我在这里在着重说一下VerifyRenderingInServerForm()这个方法,他的作用注释里都有,在这里主要是避免产生“GridView 类型 的gvProduct 不再<form runat=server> 标记中”之类的错误。详情可以查看MSDN。
2 {
3 Response.Clear();
4 Response.Buffer = true;
5 Response.Charset = "GB2312";
6 Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
7 // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
8 Response.ContentEncoding = System.Text.Encoding.UTF8;
9 Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
10 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
11 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
12 gvProduct.RenderControl(oHtmlTextWriter);
13 Response.Output.Write(oStringWriter.ToString());
14 Response.Flush();
15 Response.End();
16 }
17 //确认在运行时为指定的 ASP.NET 服务器控件呈现 HtmlForm 控件
18 public override void VerifyRenderingInServerForm(Control control)
19 { }