繁体   English   中英

如何在用户控件内将ASP网格视图导出为Excel?

[英]How to export asp grid view inside a user control to excel?

我在网页的WebUserControl中有一个GridView,但使用的是母版页,因此我需要将网格数据导出到excel和pdf。 我发现以下代码:

Response.AppendHeader("content-disposition", "attachment;filename=ExportedHtml.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
((GridView)Hosts.FindControl("hostsGrid")).RenderControl(hw);
Response.Write(tw.ToString());
Response.End();

但我收到此错误:

Control 'CPH_Body_Hosts_hostsGrid' of type 'GridView' must be placed inside 
a form tag with runat=server.

即使我在“主页”上有一个表单运行服务器。

好吧,我找到了2个解决此问题的方法:

  1. 正如King.code所评论的那样,即使GridView在表单标签中 ,它也已经在GridView中解决, 必须将其放置在带有runat =“ server”的表单标签中
  2. 我还发现此示例代码以许多其他格式导出,将GridView导出到doc / access / csv / Excel / pdf / xml / html / text / print

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM