【问题标题】:How to export two gridviews in same excel file on different worksheets?如何在不同工作表上的同一个excel文件中导出两个gridviews?
【发布时间】:2023-03-11 21:56:01
【问题描述】:

目前我的页面上有两个网格视图。我能够将一个gridview 导出到excel 文件。但是对于第二个 gridview,我希望它在同一个文件的另一个工作表中打开。 任何人都可以帮助我开始并告诉我如何去做吗?

我的代码如下所示:

protected void btnExport_Click(object sender, EventArgs e)
{
    GridView gridView = null;
    System.IO.StringWriter stringWrite = null;
    System.Web.UI.HtmlTextWriter htmlWrite = null;
    try
    {
        gridView = new GridView();
        List<string> columns = new List<string>();
        columns.Add("CREATE_AR_YN");
        columns.Add("GL_DEBIT_ACCT");
        columns.Add("ALLOC_METHOD");
        columns.Add("CUST_NAME");
        columns.Add("DEFAULT_PYMT_TERMS");
        columns.Add("AR_BILL_GROUP");

        BoundField bf = null;
        for (int i = 0; i < columns.Count; i++)
        {
            bf = new BoundField();
            bf.DataField = columns[i];
            bf.HeaderText = columns[i];
            bf.HeaderStyle.BackColor = System.Drawing.Color.FromName("#81DAF5");
            gridView.Columns.Add(bf);
            gridView.AutoGenerateColumns = false;
        }

        List<FMAProfile> custList = GetSelectedCustomerProfile();
        gridView.DataSource = custList;
        gridView.DataBind();
        Response.Clear();
        Response.ClearHeaders();
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.AddHeader("content-disposition", "attachment;filename=" + "CustomerProfile" + ".xls");
        Response.Charset = "";
        //Response.ContentType = "application/vnd.xls";
        Response.ContentType = "application/vnd.ms-excel";
        //Response.Buffer = false;
        stringWrite = new System.IO.StringWriter();
        htmlWrite = new HtmlTextWriter(stringWrite);
        gridView.RenderControl(htmlWrite);
        Response.Write(stringWrite.ToString());
        Response.End();
    }
    catch (Exception ex)
    {
        if (ex.Message.Trim().IndexOf("Thread was being aborted") == -1)
        {
            Log(ex.ToString());
        }
    }
    finally
    {
        if (gridView != null)
        {
           gridView.Dispose();
           gridView = null;
        }
        if (stringWrite != null)
        {
            stringWrite.Dispose();
        }
        stringWrite = null;
        if (htmlWrite != null)
        {
            htmlWrite.Dispose();
        }
        htmlWrite = null;
    }
}

【问题讨论】:

  • 这些是什么类型的 GridView? .NET,基础设施?你能发布一个代码示例吗?
  • 是的,它的 .net 就像我在标签中指定的那样。
  • 我刚刚发布了代码@lan
  • 这可能是重复的:stackoverflow.com/questions/7632782/…
  • @IanO'Brien 从上面的代码我怀疑这是 ASP.NET 并且在该上下文中不支持 Office 互操作(根据 MS)。

标签: c# .net excel gridview


【解决方案1】:

您导出的不是真正的 XLS,而是 Excel 可以导入的 HTML……

有关使用示例代码创建 Excel 文件的 HTML 方法的一些深入信息,请参阅http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx

也可以在http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx查看官方文档

我不确定您是否可以在 HTML 中创建多个工作表...

创建真实 Excel 文件的选项:

MS 提供 OpenXML SDK V 2.0(免费) - 请参阅 http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx

这可以读写 MS Office 文件(包括 Excel)。

另一种选择见http://www.codeproject.com/KB/office/OpenXML.aspx

如果您需要更多类似的渲染、公式等,那么有不同的免费和商业库,例如 ClosedXMLEPPlusAspose.CellsSpreadsheetGearLibXLFlexcel 等。

BEWARE: Office Interop is not supported in ASP.NET (which seems to be what your project is built in).

【讨论】:

    猜你喜欢
    • 2014-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    相关资源
    最近更新 更多