导出GridView到Excel中的关键之处
用法: ToExcel(GVStaff, TextBox1.Text);
public
static
void
ToExcel(System.Web.UI.Controlctl,
string
FileName)
...
{
HttpContext.Current.Response.Charset
=
"
UTF-8
"
;
HttpContext.Current.Response.ContentEncoding
=
System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType
=
"
application/ms-excel
"
;
HttpContext.Current.Response.AppendHeader(
"
Content-Disposition
"
,
"
attachment;filename=
"
+
""
+
FileName
+
"
.xls
"
);
ctl.Page.EnableViewState
=
false
;
System.IO.StringWritertw
=
new
System.IO.StringWriter();
HtmlTextWriterhw
=
new
HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
必须有下面这句!否则不会通过!
publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)
...
{
//ConfirmsthatanHtmlFormcontrolisrenderedfor
}
本文介绍了一种将ASP.NET中的GridView导出为Excel文件的方法。通过使用特定的HTTP上下文设置和控制渲染过程,可以实现在服务器端直接生成Excel文件的功能。
68

被折叠的 条评论
为什么被折叠?



