简体   繁体   中英

Error while exporting from a DataGridView into an Excel Worksheet

I collect some data into a DataGridView "dataGridView2"and I want to export it to a worksheet in an excel but I got an HRESULT error with error code 0x800A03EC , following is the code:

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

Please help what else can I do because I think the code above is correct and I don't why it brings out such an error Exception from HRESULT: 0x800A03EC

Try replacing new.Microsoft.Office.Interop.Excel.Application(); with new Microsoft.Office.Interop.Excel.Application();

HTH!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM