2016-08-20 161 views
2

此代碼在導出excel後總是跳過最後一行,你能檢查代碼中的錯誤嗎?將Datagridview導出到Excel中的Excel#

我改變

transcationTableDataGridView.Rows.Count - 1 

transcationTableDataGridView.Rows.Count + 1 

它導出所有行到excel,但拋出指數應該是非負的錯誤此異常:enter image description here

private void exportToExcel_Click(object sender, EventArgs e) 
     { 
try 
      { 
       Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
       Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
       Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
       app.Visible = true; 
       worksheet = workbook.Sheets["Sheet1"]; 
       worksheet = workbook.ActiveSheet; 
       worksheet.Name = "Records"; 

       try 
       { 
        for (int i = 1; i < transcationTableDataGridView.Columns.Count + 1; i++) 
        { 
         worksheet.Cells[1, i] = transcationTableDataGridView.Columns[i - 1].HeaderText; 
        } 
        for (int i = 0; i < transcationTableDataGridView.Rows.Count - 1; i++) 
        { 
         for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++) 
         { 
          if (transcationTableDataGridView.Rows[i].Cells[j].Value != null) 
          { 
           worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString(); 
          } 
          else 
          { 
           worksheet.Cells[i + 2, j + 1] = ""; 
          } 
         } 
        } 

        //Getting the location and file name of the excel to save from user. 
        SaveFileDialog saveDialog = new SaveFileDialog(); 
        saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; 
        saveDialog.FilterIndex = 2; 

        if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
        { 
         workbook.SaveAs(saveDialog.FileName); 
         MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
        } 
       } 
       catch (System.Exception ex) 
       { 
        MessageBox.Show(ex.Message); 
       } 

       finally 
       { 
        app.Quit(); 
        workbook = null; 
        worksheet = null; 
       }  
      } 
      catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } 
     } 

這也給了我一些電腦

我使用這個代碼導出過程中的錯誤:這個代碼

using Excel=Microsoft.Office.Interop.Excel; 

using System.Runtime.InteropServices; 

誰能解釋什麼是兩者之間的區別?

+0

當您在Used PC中使用Microsoft.Office.Interop.Excel庫時必須具有Microsoft Excel程序。也許沒有Excel程序當前比較 –

回答

5
private void exportToExcel_Click(object sender, EventArgs e) 
    { 
     try 
     { 
      Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); 
      Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); 
      Microsoft.Office.Interop.Excel._Worksheet worksheet = null; 
      app.Visible = true; 
      worksheet = workbook.Sheets["Sheet1"]; 
      worksheet = workbook.ActiveSheet; 
      worksheet.Name = "Records"; 

      try 
      { 
       for (int i = 0; i < transcationTableDataGridView.Columns.Count; i++) 
       { 
        worksheet.Cells[1, i + 1] = transcationTableDataGridView.Columns[i].HeaderText; 
       } 
       for (int i = 0; i < transcationTableDataGridView.Rows.Count; i++) 
       { 
        for (int j = 0; j < transcationTableDataGridView.Columns.Count; j++) 
        { 
         if (transcationTableDataGridView.Rows[i].Cells[j].Value != null) 
         { 
          worksheet.Cells[i + 2, j + 1] = transcationTableDataGridView.Rows[i].Cells[j].Value.ToString(); 
         } 
         else 
         { 
          worksheet.Cells[i + 2, j + 1] = ""; 
         } 
        } 
       } 

       //Getting the location and file name of the excel to save from user. 
       SaveFileDialog saveDialog = new SaveFileDialog(); 
       saveDialog.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; 
       saveDialog.FilterIndex = 2; 

       if (saveDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
       { 
        workbook.SaveAs(saveDialog.FileName); 
        MessageBox.Show("Export Successful", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); 
       } 
      } 
      catch (System.Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

      finally 
      { 
       app.Quit(); 
       workbook = null; 
       worksheet = null; 
      } 
     } 
     catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } 
    } 
+0

謝謝,它的工作原理,你能提供答案我的底部問題? – Patrick

+0

當您在Used PC中使用Microsoft.Office.Interop.Excel庫時,必須安裝Microsoft Excel程序。也許沒有Excel程序目前比較 –