簡體   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