0

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!

Behzad
  • 3,502
  • 4
  • 36
  • 63
CodeMonkey
  • 2,511
  • 4
  • 27
  • 38
  • do you have this line in your using at the top of the class file `using System.Runtime.InteropServices;` also make sure that you manually add the reference to the references node manually.. – MethodMan Jun 09 '15 at 17:47
  • You are going to regret using excel automation in the long run. Instead use a third party library that can write excel files. – Magnus Jun 09 '15 at 17:48
  • Similar problem: http://stackoverflow.com/questions/3003719/hresult-0x80040154-regdb-e-classnotreg – Francine DeGrood Taylor Jun 09 '15 at 17:48
  • @MethodMan i added that and it didnt work! – CodeMonkey Jun 09 '15 at 17:51
  • @Magnus i already have but i have no choice ! im a noobi and i need something easy to grasp and comprehend which this seems like the easiest way – CodeMonkey Jun 09 '15 at 17:53
  • what is your project setup to run as ..anyCPU, x86.., etc..? also see if not using `regsvr32.exe` helps solve the problem.. running the same code on my local machine works just fine – MethodMan Jun 09 '15 at 17:56
  • @MethodMan i had to set it as x86...but how can i check if im using regsvr32.exe or not?? – CodeMonkey Jun 09 '15 at 17:59
  • there are command line help that you can use try `regsvr32 ?` from the command line and see what the options are.. in .net honestly you shouldn't have to do this.. – MethodMan Jun 09 '15 at 18:00
  • @MethodMan i tried to check it with regsvr32 ? in command line and i got an error saying : the module "?" failed to load. make sure the binary is stored at the specified path or debug it to check for more problems with the binary or dependent .DLL files. the specified module could not be found – CodeMonkey Jun 09 '15 at 18:06
  • what version of VS are you using..? – MethodMan Jun 09 '15 at 18:07
  • @MethodMan VS Ultimate 2013 with Update 2 32-Bit – CodeMonkey Jun 09 '15 at 18:10
  • @Magnus can you name me a few of the third party libraries which you know of? – CodeMonkey Jun 09 '15 at 18:37
  • 1
    I've used [Aspose.Cells](http://www.aspose.com/.net/excel-component.aspx) (costs money, but very good). But you can also use [EPPlus](http://epplus.codeplex.com/) or check this SO answer on a similar question: http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp – Magnus Jun 09 '15 at 19:38

0 Answers0