【问题标题】:How do I export 2 gridviews into 2 separate sheets in one MS Excel file?如何将 2 个网格视图导出到一个 MS Excel 文件中的 2 个单独的工作表中?
【发布时间】:2015-09-27 12:46:35
【问题描述】:

如何通过单击 1 个按钮将 2 个网格视图(GridView1 和 GridView2)导出到一个 MS Excel 文件中的 2 个单独的工作表中?目前,我只能将 1 个网格视图导出到文件名与工作表名称相同的 Excel 工作表。但我想将 2 个网格视图分成 2 个单独的工作表,我希望自己定义/设置工作表名称。谢谢

public void ExportGridToExcel()
{
      Response.Clear();
      Response.Buffer = true;
      Response.ClearContent();
      Response.ClearHeaders();
      Response.Charset = "";

      string FileName ="Export"+DateTime.Now+".xls";

      StringWriter strwritter = new StringWriter();
      HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);

      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.ContentType = "application/vnd.ms-excel";
      Response.AddHeader("Content-Disposition","attachment;filename=" + FileName);

      GridView1.GridLines = GridLines.Both;
      GridView1.HeaderStyle.Font.Bold = true;
      GridView1.RenderControl(htmltextwrtter);

      Response.Write(strwritter.ToString());
      Response.End();
}

【问题讨论】:

标签: c# asp.net excel gridview


【解决方案1】:

您需要修改您的代码,以便在 excel 文件中创建一个工作表并手动将数据从网格复制到 excel 文件,并将此代码创建为一个函数,该函数采用网格、工作表名称和工作表 ID,如 1 ,2 等on 作为参数,然后在按钮单击事件中调用它两次,一次用于 grid1,一次用于 grid2,分别具有不同的工作表名称和工作表 ID 1 和 2。函数代码如下。

在按钮单击事件中编写这两行代码,然后通过将 excel 应用程序和工作簿也作为参数传递给每个网格调用两次函数。

     // creating Excel Application
     Microsoft.Office.Interop.Excel._Application app  = new Microsoft.Office.Interop.Excel.Application();

    // creating new WorkBook within Excel application
    Microsoft.Office.Interop.Excel._Workbook workbook =  app.Workbooks.Add(Type.Missing);

 public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook,GridView gridview,string SheetName,int sheetid)
 {  


        // creating new Excelsheet in workbook
         Microsoft.Office.Interop.Excel._Worksheet worksheet = null;                   

       // see the excel sheet behind the program
        app.Visible = true;

       // get the reference of first sheet. By default its name is Sheet1.
       // store its reference to worksheet
        worksheet = workbook.Sheets["Sheet"+ sheetid];
        worksheet = workbook.ActiveSheet;

        // changing the name of active sheet
        worksheet.Name = sheetname; 

        // storing header part in Excel
        for(int i=1;i<gridview.Columns.Count+1;i++)
        {
              worksheet.Cells[1, i] = gridview.Columns[i-1].HeaderText;
        }



        // storing Each row and column value to excel sheet
        for (int i=0; i < gridview.Rows.Count-1 ; i++)
        {
            for(int j=0;j<gridview.Columns.Count;j++)
            {
                worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Value.ToString();
            }
        }


        // save the application
        workbook.SaveAs("c:\\output.xls",Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive , Type.Missing, Type.Missing, Type.Missing, Type.Missing);

        // Exit from the application
      app.Quit();
    }

【讨论】:

  • 我想将多个gridview导出到一个excel,因此我相信你的代码将通过调用两次导出到2个单独的excel中。请指教谢谢
  • 对不起。我编辑了我的答案。希望这能解决问题。
  • 我已添加 Microsoft.Office.Interop.Excel 作为参考,但其中一个 DataGridView 仍然存在错误,即下划线缺少类型或命名空间(缺少参考)错误。请指教
  • 请提供您遇到错误的确切行和错误消息
  • public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook,DataGridView gridview,string SheetName,int sheetid)
【解决方案2】:

//编辑我的答案,因为只有 1 个导出按钮: 看看this question

首先添加这些命名空间:

using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;

所以基本上你应该拥有(或继续制作它们)2 个 DataTable(绑定到 2 个网格视图):dt1 和 dt2

创建一个数据集并向其中添加 2 个数据表

DataSet dataset = new DataSet();
        dataset.Tables.Add(dt1);
        dataset.Tables.Add(dt2);

然后

//Print using Ofice InterOp
    Excel.Application excel = new Excel.Application();

    var workbook = (Excel._Workbook)(excel.Workbooks.Add(Missing.Value));

    for (var i = 0; i < dataset.Tables.Count; i++)
    {

            if (workbook.Sheets.Count <= i)
            {
                workbook.Sheets.Add(Type.Missing, Type.Missing, Type.Missing,
                                    Type.Missing);
            }

            //NOTE: Excel numbering goes from 1 to n
            var currentSheet = (Excel._Worksheet)workbook.Sheets[i + 1]; 

            for (var y = 0; y < dataset.Tables[i].Rows.Count; y++)
            {
                for (var x = 0; x < dataset.Tables[i].Rows[y].ItemArray.Count(); x++)
                {
                    currentSheet.Cells[y+1, x+1] = dataset.Tables[i].Rows[y].ItemArray[x];
                }
            }
    }

    string outfile = @"C:\APP_OUTPUT\EXCEL_TEST.xlsx";

    workbook.SaveAs( outfile, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing, Type.Missing, Excel.XlSaveAsAccessMode.xlNoChange,
                    Type.Missing, Type.Missing, Type.Missing, Type.Missing,
                    Type.Missing);

    workbook.Close();
    excel.Quit();

要更改工作表名称,我相信你可以做到:

currentSheet.Name = "your sheet name"

【讨论】:

  • 你好,只有一个导出按钮。
猜你喜欢
  • 2017-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多