Hi, my first programming query: well three queries. All I hope are trivial :)

I thought I would have a go at trying to retrieve a value from an existing excel sheet.

To do this I have used Project...Add reference to add
- Microsoft Excel 12.0 Object library
- Microsoft Excel 5.0 Object Library
Which one should I use?

This has given me in the project References:
- Excel
- Microsoft.Office.Core
- Microsoft.Office.Interop.Excel
Looking at Excel and ...Interop.Excel they seem to be fairly similar. What is the difference and which one should I use?

I have put in my programme

using Excel;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            Excel.Application xl = new Excel._ExcelApplicationClass();

On the line ...= new... I get the error:Error 1 Cannot implicitly convert type 'Excel._ExcelApplicationClass' to 'Excel.Application'. An explicit conversion exists (are you missing a cast?)

What am I doing wrong?

>.Add reference to add

MS-Excel 5.0 or 7.0, select the Microsoft Excel 5.0 Object Library.
MS-Excel 97, select the Microsoft Excel 8.0 Object Library.
MS-Excel 2000, select the Microsoft Excel 10.0 Object Library.
MS-Excel 2003, select the Microsoft Excel 11.0 Object Library.
MS-Excel 2007, select the Microsoft Excel 12.0 Object Library.

>What is the difference and which one should I use?

You have added references of 5.0 and 12.0 lib. Add product specific lib. reference.

>Cannot implicitly convert type 'Excel._ExcelApplicationClass' to 'Excel.Application'

Application is an interface where as _ExcelApplicationClass is a class.

Excel.Application x1=(Excel.Application) new Excel._ExcelApplicationClass();

Have a look at msdn page - http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.aspx

Comments
Good explanatiion!

Thanks for that.
I corrected the references to use just 12.0 Object Library

using Excel.Application x1=(Excel.Application) new Excel._ExcelApplicationClass(); I got

Error 1 The type or namespace name '_ExcelApplicationClass' does not exist in the namespace 'Microsoft.Office.Interop.Excel' (are you missing an assembly reference?)

I changed it to: Excel.Application xl=(Excel.Application) new Excel.ApplicationClass();which is working.

But I am struggling with the concept of what the line is saying. ie if I use string myStr = "QWERT"; This says create a variable named myStr to contain data of type string and create it with the value QWERTY in it.

So the line above is saying: create a variable named xl to contain data of type Excel.Application and create it with a new Excel.ApplicationClass(). But what does the =(Excel.Application) do?

Edited 3 Years Ago by Dani: Formatting fixed

>I corrected the references to use just 12.0 Object Library

Classes/interfaces of Excel namespace are belongs to older version (5.0). Remove the reference of use Excel (5.0).

Use,

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

Have a look, here are some links (of tutorials).

1. http://csharp.net-informations.com/excel/csharp-excel-tutorial.htm
2. http://msdn.microsoft.com/en-us/library/ms173186%28VS.80%29.aspx
3. http://msdn.microsoft.com/en-us/library/aa537184%28office.11%29.aspx

Comments
a good help

Firstly, its [icode]code here[/icode] for inline code :)

Secondly, the (Excel.Application) is a cast. It tells the compiler to convert the object after it into the type within the brackets. A word of caution: if the object to be converted cannot be cast to the given type it will throw an exception.

Edited 3 Years Ago by Dani: Formatting fixed

Comments
That makes perfect sense. Cheers.

Thanks, that makes perfect sense. I do like to know why, not just how.

Thanks, that makes perfect sense. I do like to know why, not just how.

That will serve you well. Too many people copy and paste working code without ever stopping to look at what it does and how it does it. By learning what it is doing you can apply that understanding to problems in the future :)

This question has already been answered. Start a new discussion instead.