2015-09-27 21 views
0

如何在一个MS Excel文件中通过单击1按钮将2个网格视图(GridView1 & GridView2)导出到2张独立的工作表中?目前,我只能将一个gridview导出到一个Excel工作表,其名称与工作表名称相同。但是我想将2个gridview分成2个单独的表格,我希望表单名称可以由我自己定义/设置。由于如何在一个MS Excel文件中将2个gridview导出为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(); 
} 
+0

我建议你使用EP Plus(http://epplus.codeplex.com/)等库来做到这一点。 – navigator

回答

0

你需要modifiy你的代码,以便您将创建Excel文件中的工作表并手动将数据从网格复制到excel文件中,并将该代码创建为以grid,sheetname和sheetid(如1,2等)为参数的函数,然后在button click事件中为grid1调用两次一次对于grid2分别使用不同的sheetnames和sheet 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(); 
    } 
+0

我想导出多个gridview到一个excel,因此我相信你的代码会通过调用两次导出到两个独立的excel中。请建议谢谢 –

+0

对不起。我编辑了我的答案。希望这将解决这个问题。 –

+0

我已经添加了Microsoft.Offfice.Interop.Excel作为参考,但DataGridView中仍然有一个错误是下划线缺少类型或名称空间(缺少引用)错误。请咨询 –

0

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

首先添加这些命名空间:

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

所以基本上你应该有(或者继续制作它们)2 DataTable(绑定到2个gridviews):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" 
+0

嗨,只有一个导出按钮。 –

相关问题