/// <summary>
/// 导出显示数据Execl
/// </summary>
/// <param name="ctl">控件ID</param>
public void Excel(System.Web.UI.Control ctl)
{
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "gb2312";//UTF-8
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
string ss = "<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/" />";//加入编码
string DC = ss+tw.ToString();
HttpContext.Current.Response.Write(DC);
HttpContext.Current.Response.End();
}
导出显示数据的Execl
最新推荐文章于 2025-12-01 09:52:16 发布
本文介绍了一种使用ASP.NET将控件内容导出为Excel文件的方法。通过设置HTTP响应头并利用StringWriter和HtmlTextWriter,可以实现将网页表单数据格式化并导出到Excel的功能。

1222

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



