Exporting datagridview to excel file failed

322 views Asked by At

I'm trying to export datagridview to excel file but I keep getting this error:

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll

Additional information: Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error:

80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

This is my code:

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

Workbook wb = excel.Workbooks.Add(XlSheetType.xlWorksheet);
Worksheet ws = (Worksheet)excel.ActiveSheet;
excel.Visible = true;
for (int j = 0; j <= dataGridView1.Rows.Count - 1; j++)
{
    for (int i = 0; i <= 5; i++)
    {
        ws.Cells[j, i] = dataGridView1.Rows[j].Cells[i].Value;
    }
}

I get the error on this line of my code :

Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

Now I have already added Microsoft.Office.Interop.Excel; version 14.0.0.0 and I have configured my build to x86 but I'm still getting this error! I'm not sure what am I doing wrong!

0

There are 0 answers