2015-09-27 47 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

嗨,只有一個導出按鈕。 –

相關問題