【发布时间】:2011-12-20 03:26:32
【问题描述】:
我有 2 个网格视图,它们的作用类似于产品类别和产品目录。 每个类别都有少量产品。
现在用户选择了类别并查看了产品目录后,我想将这两个导出到excel中
示例: 所选产品类别:“水果” 目录是:“苹果、香蕉、橙子”
我想存储选择的产品类别以及目录。
目前,我只能导出产品目录列表。(简单地将1个gridview导出到excel)
我希望我说清楚。
我在asp.net c#上运行
按钮点击的当前代码是
string filename = String.Format("Survey Results_{0}_{1}.xls",
DateTime.Today.Month.ToString(), DateTime.Today.Year.ToString());
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.Charset = "";
// SetCacheability doesn't seem to make a difference (see update)
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
// Replace all gridview controls with literals
ClearControls(GridView2);
// Throws exception: Control 'ComputerGrid' of type 'GridView'
// must be placed inside a form tag with runat=server.
// ComputerGrid.RenderControl(htmlWrite);
// Alternate to ComputerGrid.RenderControl above
System.Web.UI.HtmlControls.HtmlForm form
= new System.Web.UI.HtmlControls.HtmlForm();
Controls.Add(form);
form.Controls.Add(GridView2);
form.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
【问题讨论】:
-
自己解决了。我想出了一个解决方案,将 excel 文件名作为选定的“产品类别”。
标签: asp.net excel gridview export