2016-05-16 65 views
-1

我在我的DataGridView中有很多行和8列。我正在使用MySQL數據庫,我只想將DataGridView的兩個或三個選定列數據導出到一個Excel工作表,並且其按鈕單擊按鈕。我已經試過了這段代碼,但是代碼複製了所有的列與標題。 請給我的代碼,所以我會做我的任務。 謝謝如何將選定的datagidview列導出到C#窗體中的excel表格中

這裏是我的代碼:但是這段代碼複製excel中帶有out標題的所有列。

private void btnExport_Click(object sender, EventArgs e) 
    { 
     Excel.Application xlApp; 
     Excel.Workbook xlWorkBook; 
     Excel.Worksheet xlWorkSheet; 
     object misValue = System.Reflection.Missing.Value; 

     Int16 i, j; 

     xlApp = new Excel.Application(); 
     xlWorkBook = xlApp.Workbooks.Add(misValue); 

     xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); 

     for (i = 0; i <= dgvSlipRecord.RowCount - 2; i++) 
     { 
      for (j = 0; j <= dgvSlipRecord.ColumnCount - 1; j++) 
      { 
       xlWorkSheet.Cells[i + 1, j + 1] = dgvSlipRecord[j, i].Value.ToString(); 
      } 
     } 

     xlWorkBook.SaveAs(@"d:\csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     releaseObject(xlWorkSheet); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
    } 

    private void releaseObject(object obj) 
    { 
     try 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); 
      obj = null; 
     } 
     catch (Exception ex) 
     { 
      obj = null; 
      MessageBox.Show("Exception Occured while releasing object " + ex.ToString()); 
     } 
     finally 
     { 
      GC.Collect(); 
     } 
    } 
+0

請向我們展示您所寫的代碼,以便我們幫您解決問題。 – OldBoyCoder

+0

請看我更新過的代碼。 –

回答

0

要如何決定選擇要導出的列,所以我已經創建列索引的一個簡單的集合稱爲ColumnsToInclude你不說清楚。然後我用它來引用dgv集合。我已經添加了一個循環來將列標題添加到輸出。希望這將足以讓你完成你的colution。

private void btnExport_Click(object sender, EventArgs e) 
    { 

     Microsoft.Office.Interop.Excel.Application xlApp; 
     Workbook xlWorkBook; 
     Worksheet xlWorkSheet; 
     object misValue = System.Reflection.Missing.Value; 
     int[] ColumnsToInclude = { 0, 2 }; 
     Int16 i; 

     xlApp = new Microsoft.Office.Interop.Excel.Application(); 
     xlWorkBook = xlApp.Workbooks.Add(misValue); 

     xlWorkSheet = (Worksheet)xlWorkBook.Worksheets.get_Item(1); 
     for (int ix = 0; ix < ColumnsToInclude.Length; ix++) 
      xlWorkSheet.Cells[1, ix + 1] = dgvSlipRecord.Columns[ColumnsToInclude[ix]].HeaderText; 
     for (i = 0; i <= dgvSlipRecord.RowCount - 2; i++) 
     { 
      for (int j = 0; j < ColumnsToInclude.Length; j++) 
      { 
       xlWorkSheet.Cells[i + 2, j + 1] = dgvSlipRecord[ColumnsToInclude[j], i].Value.ToString(); 
      } 
     } 

     xlWorkBook.SaveAs(@"c:\temp\csharp.net-informations.xls", XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue); 
     xlWorkBook.Close(true, misValue, misValue); 
     xlApp.Quit(); 

     releaseObject(xlWorkSheet); 
     releaseObject(xlWorkBook); 
     releaseObject(xlApp); 
    } 
+0

謝謝它的工作 –

相關問題