title: Exporting Datagridview to Excel in C#
body: <p>This code always skips last row after exporting excel, can you check what's wrong in the code?</p>
  
  <p><strong>I changed</strong></p>
  
  <pre><code>transcationTableDataGridView.Rows.Count - 1
  </code></pre>
  
  <p><strong>to</strong> </p>
  
  <pre><code>transcationTableDataGridView.Rows.Count + 1
  </code></pre>
  
  <p>it does export all rows to excel but throws index should be non-negative error this exception: <a href="https://i.stack.imgur.com/y8Wxu.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/y8Wxu.jpg" alt="enter image description here"></a></p>
  
  <pre><code> 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 &lt; transcationTableDataGridView.Columns.Count + 1; i++)
                      {
                          worksheet.Cells[1, i] = transcationTableDataGridView.Columns[i - 1].HeaderText;
                      }
                      for (int i = 0; i &lt; transcationTableDataGridView.Rows.Count - 1; i++)
                      {
                          for (int j = 0; j &lt; 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()); }
          }
  </code></pre>
  
  <p><strong>It also gives me error during export in some computers</strong></p>
  
  <p>I was using this code:</p>
  
  <pre><code>using Excel=Microsoft.Office.Interop.Excel;
  </code></pre>
  
  <p>to this code: </p>
  
  <pre><code>using System.Runtime.InteropServices;
  </code></pre>
  
  <p>can anyone explain what is the difference between two?</p>