Exporting Datagridview to Excel in C#

2019-07-01 10:23发布

This code always skips last row after exporting excel, can you check what's wrong in the code?

I changed

transcationTableDataGridView.Rows.Count - 1

to

transcationTableDataGridView.Rows.Count + 1

it does export all rows to excel but throws index should be non-negative error this exception: 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()); }
        }

It also gives me error during export in some computers

I was using this code:

using Excel=Microsoft.Office.Interop.Excel;

to this code:

using System.Runtime.InteropServices;

can anyone explain what is the difference between two?

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-01 10:56
    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()); }
    }
查看更多
▲ chillily
3楼-- · 2019-07-01 10:58

i have one repiring to out full data to excl you can use this code private void button22_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 < dataGridView3.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dataGridView3.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dataGridView3.Rows.Count - 0; i++)
                {
                    for (int j = 0; j < dataGridView3.Columns.Count; j++)
                    {
                        if (dataGridView3.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 2, j + 1] = dataGridView3.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());
        }
    }strong text
查看更多
登录 后发表回答