将2 gridview导出到excel
我有 2 个 gridview,其作用类似于产品类别和产品目录。 每个类别都有很少的产品。
现在,在用户选择类别并查看产品目录后,我想在 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();
I have 2 gridview which works like product category and product catalog.
each category have few products.
Now After the user have selected the category and view the product catalog, I would like to export this two in the excel
Example :
selected Product category: "Fruit"
the catalog is : "Apple, Banana,Orange"
I want to store the selected product category as well as the catalog.
For now, I only able to export the list of product catalog.(simple exporting of 1 gridview to excel)
I hope i make myself clear.
im running on asp.net c#
currenct code on the button click is
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以尝试通过加入结果在
代码隐藏
中构建动态gridview
,而不是输出gridview2
,您将必须导出动态生成的<代码>gridview。Maybe you can try building a dynamic
gridview
in thecodebehind
by joining the results, and instead of outputtinggridview2
you will have to export dynamically generatedgridview
.