将 datagridview 数据导出到 Excel 工作表

时间:2021-03-22 11:53:22

标签: c# excel visual-studio

我正在处理一个项目,我需要将 datagridview 数据导出到 Excel 表中。我的 datagridview 有 38 列和大约 120 行(单词之间有空格),数据是从 datagridview 输入的。当我运行应用程序时,我收到此异常,“未将对象引用设置为对象的实例。”。这是我的代码块:

private void exportbtn_Click(object sender, EventArgs e)

        if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
        {
            Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
            ExcelApp.Application.Workbooks.Add(Type.Missing);
            ExcelApp.Columns.AutoFit();

            for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
            {
                ExcelApp.Cells[1, i] = dataGridView1.Columns[i - 1].HeaderText;
            }

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView1.Columns.Count; j++)
                {
                   ExcelApp.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                }
                MessageBox.Show("Data Exported Successfully");
            }
            ExcelApp.ActiveWorkbook.SaveAs("");
            ExcelApp.ActiveWorkbook.Saved = true;              
            ExcelApp.Visible = true;
            ExcelApp.Quit();
         
         }

我将 datagridview 的第一列(设施)填充为:

        ArrayList row = new ArrayList();
        row.Add("Techiman Holy Family Hospital"); dataGridView1.Rows.Add(row.ToArray());
        row = new ArrayList();
        row.Add("Opoku Agyeman Hospital"); dataGridView1.Rows.Add(row.ToArray());
        row = new ArrayList();
        row.Add("Kintampo Municipal Hospital"); dataGridView1.Rows.Add(row.ToArray());

此外,我不想在导出到 excel 时随时选择新的 excel 工作簿。我想将所有工作表保存在同一个工作簿中。有没有办法编辑以下代码来实现这一点:

        saveFileDialog1.InitialDirectory = ("C:");
        saveFileDialog1.Title = "Save as Excel File";
        saveFileDialog1.FileName = "";
        saveFileDialog1.Filter = "Excel Files(2003)|*.xls| Excel Files(2007)|*.xlsx";

请问有什么帮助吗?谢谢。

1 个答案:

答案 0 :(得分:0)

首先,我建议你可以参考下面的代码给你的datagridview添加数据。

 private void Form1_Load(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table.Columns.Add("Hospital");
            table.Columns.Add("Name");
            table.Columns.Add("Age");
            table.Columns.Add("Address");
            table.Columns.Add("Id");
            table.Columns.Add("Sex");
            table.Rows.Add("Techiman Holy Family Hospital", "test11", "22","home","1001","Man");
            table.Rows.Add("Techiman Holy Family Hospital", "test2", "23", "home", "1001", "Man");
            table.Rows.Add("Opoku Agyeman Hospital", "test8", "22", "home", "1001", "Man");
            table.Rows.Add("Opoku Agyeman Hospital", "test6", "26", "home", "1001", "Man");
            table.Rows.Add("Kintampo Municipal Hospital", "test21", "22", "home", "1001", "Man");
            table.Rows.Add("Kintampo Municipal Hospita", "test12", "22", "home", "1001", "Man");
            dataGridView1.DataSource = table;
        }

第二,解决问题的关键是我们需要把dataGridView1.Rows.Count改成dataGridView1.Rows.Count-1,因为datagirdview会自动生成一个空行。

最后,这里有一个代码示例,您可以参考。

 private void button1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.InitialDirectory = ("E:");
            saveFileDialog1.Title = "Save as Excel File";
            saveFileDialog1.FileName = "";
            saveFileDialog1.Filter = "Excel Files(2003)|*.xls| Excel Files(2007)|*.xlsx";

            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                Excel.Application ExcelApp = new Excel.Application();
                ExcelApp.Application.Workbooks.Add(Type.Missing);
                ExcelApp.Columns.AutoFit();
                Excel.Worksheet sheet = ExcelApp.ActiveSheet;
                for (int i = 0; i < dataGridView1.Columns.Count; i++)
                {
                    sheet.Cells[1, i+1] = dataGridView1.Columns[i].HeaderText;
                }

                for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
                {
                    for (int j = 0; j < dataGridView1.Columns.Count; j++)
                    {
                        sheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
                    }
                    
                }
                MessageBox.Show("Data Exported Successfully");
                sheet.SaveAs(saveFileDialog1.FileName);
                ExcelApp.Visible = true;
                ExcelApp.Quit();
                
            }
        }
相关问题