将2 gridview导出到excel

发布于 2024-12-21 22:23:37 字数 1495 浏览 2 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

霊感 2024-12-28 22:23:37

也许您可以尝试通过加入结果在代码隐藏中构建动态gridview,而不是输出gridview2,您将必须导出动态生成的<代码>gridview。

Maybe you can try building a dynamic gridview in the codebehind by joining the results, and instead of outputting gridview2 you will have to export dynamically generated gridview.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文