C# -Excel interoperability

Viewed 26187

I want to call Excel Sheet from C# 4.0 (VS 2010 Express Edition) .

When i declare ,

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

excel.Visible = true;

I receive error as

Interop type 'Microsoft.Office.Interop.Excel.ApplicationClass' cannot be embedded. Use the applicable interface instead.

What is the soultion ?

6 Answers

Here is a blog post that deals with that. Looks like you need to change

Microsoft.Office.Interop.Excel.ApplicationClass();

to

Microsoft.Office.Interop.Excel.Application();

You need to declare the variable as Microsoft.Office.Interop.Excel.Application, but instantiate it as Microsoft.Office.Interop.Excel.ApplicationClass.

Excel.Application = new Excel.ApplicationClass();

Note the leading Excel.Application, not Excel.ApplicationClass.

Also note, this is straight out of the MSDN page for ApplicationClass.

For MS Office 2016 you need to use the following no dramas

Excel.Application oExcel = new Excel.Application();
Related