在C#中导出Datagridview到Excel

这段代码总是在导出excel后跳过最后一行,你能检查代码中的错误吗?

我变了

transcationTableDataGridView.Rows.Count - 1 

 transcationTableDataGridView.Rows.Count + 1 

它确实将所有行导出为ex​​cel,但是引发索引应该是非负的错误。 在这里输入图像描述

  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; 

任何人都可以解释两者之间有什么区别?

  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()); } }