2013-08-26 47 views

回答

1

如果你談論的是數據網格視圖中,可以這樣做:

public void ExportToExecl(DataGridView dg, string filename) 
    { 
     // 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); 
     // creating new Excelsheet in workbook 
     Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
     // get the reference of first sheet. By default its name is Sheet1. 
     // store its reference to worksheet 
     try 
     { 
      worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets["Sheet1"]; 
      worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet; 
      // changing the name of active sheet 
      worksheet.Name = "Exported from History Parsing"; 
      // storing header part in Excel 
      for (int i = 1; i < dg.Columns.Count + 1; i++) 
      { 
       worksheet.Cells[1, i] = dg.Columns[i - 1].HeaderText; 
       worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray); 
      } 
      // storing Each row and column value to excel sheet 
      for (int i = 0; i < dg.Rows.Count - 1; i++) 
      { 
       for (int j = 0; j < dg.Columns.Count; j++) 
       { 
        worksheet.Cells[i + 2, j + 1] = dg.Rows[i].Cells[j].Value.ToString(); 
        worksheet.Cells[i + 2, j + 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Gray); 
       } 
      } 
      // save the application 
      workbook.SaveAs(filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 
      MessageBox.Show("Your excel file was created successfully"); 
     } 
     catch (System.Exception ex) 
     { 
     } 
     finally 
     { 
      app.Quit(); 
      workbook = null; 
      app = null; 
     } 
    } 

你也可以閱讀有關出口DGV這裏擅長:

http://www.codeproject.com/Articles/28269/Exporting-a-DataGridView-to-an-Excel-PDF-image-fil

這裏:

http://www.codeproject.com/Articles/43400/Generalized-DataGridView-Export-to-Excel-with-Them

  • 在編寫代碼之前,您必須添加對Microsoft Excel對象庫的引用。 右鍵單擊您的項目並選擇添加引用菜單。之後,轉到COM選項卡並選擇並添加Microsoft Excel 12.0對象庫
相關問題