从DataGridView导出到Excel工作表时出错

本文关键字:工作 出错 Excel DataGridView | 更新日期: 2023-09-27 17:58:52

我将一些数据收集到DataGridView"dataGridView2"中,并想将其导出到excel中的工作表中,但我遇到了HRESULT错误,错误代码为0x800A03EC,以下是代码:

private void button2_Click(object sender, EventArgs e)
{
     Microsoft.Office.Interop.Excel.Application app = new.Microsoft.Office.Interop.Excel.Application();
     Microsoft.Office.Interop.Excel.Workbook workbook = app.Workbooks.Open(@path);
     Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
    //Couunt the number cells in the
    //datagridview asign it to an integers
    //variable
    int rHtcount = dataGridView2.Rows.Count;
    int iii = 0;
    //put each values from each cells of the
    //datagridview into each cell in the
    //destination
    for(; iii < rHtcount; iii++)
    {
         worksheet.Cells[iii + 2, "H"].Value = dataGridView2.Rows[iii].Cells[0].Value;
    }
    app.Visible = true;
    workbook.Close();
}

请帮助我还能做些什么,因为我认为上面的代码是正确的,我不知道为什么它会出现这样的错误Exception from HRESULT: 0x800A03EC

从DataGridView导出到Excel工作表时出错

尝试替换new.Microsoft.Office.Interop.Excel.Application();new Microsoft.Office.Interop.Excel.Application();

啊!!!