Running Excel from c#

  • Thread starter Thread starter Motty
  • Start date Start date
M

Motty

Hi perhaps someone can help me.

I've been trawling the boards to find a way of starting Excel from C#.
So far it seems I need to use

using System.Runtime.InteropServices;
using Excel;

Problem is the 'using Excel' fails to work, with the error.

The type or namespace name 'Excel' could not be found (are you missing
a using directive or an assembly reference?)

I've no idea whats wrong (I do use the standard edition of c# tho).

I'd prefer it if I could access Excel using these methods, for the
reasons of reating tables etc. If it's not possible...

....How do I run Excel from c#?

thanks,

Alan Raby
 
M> I've been trawling the boards to find a way of starting Excel from C#.
M> So far it seems I need to use
M> using System.Runtime.InteropServices;
M> using Excel;
M> Problem is the 'using Excel' fails to work, with the error.
M> The type or namespace name 'Excel' could not be found (are you missing
M> a using directive or an assembly reference?)
M> I've no idea whats wrong (I do use the standard edition of c# tho).
M> I'd prefer it if I could access Excel using these methods, for the
M> reasons of reating tables etc. If it's not possible...
M> ...How do I run Excel from c#?

try project menu -> add reference -> com -> microsoft excel x.x object
library -> select -> ok
 
If you are using Office XP, you will want to install the Office XP Primary Interop Assemblies

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnoxpta/html/odc_oxppias.as
http://www.microsoft.com/downloads/...61E-3060-4F71-A6B4-01FEBA508E52&displaylang=e

Then, you can add a reference the Excel using the normal add Add Reference dialog. Select the COM tab and scroll down to and add the Microsoft Excel 10.0 / 11.0 etc entry (depends on which version you have installed)

Then, in your code, you will be able to add

using Excel
using Microsoft.Office.Core
etc..

Excel._Application excel = new Excel.ApplicationClass( )
excel.Visible = true

HT

RS
 
Mottywrote:
Hi perhaps someone can help me.
I've been trawling the boards to find a way of starting Excel from C#.
So far it seems I need to use

using System.Runtime.InteropServices;
using Excel;

Problem is the 'using Excel' fails to work, with the error.

The type or namespace name 'Excel' could not be found (are you missing
a using directive or an assembly reference?)

I've no idea whats wrong (I do use the standard edition of c# tho).

I'd prefer it if I could access Excel using these methods, for the
reasons of reating tables etc. If it's not possible...

....How do I run Excel from c#?

thanks,

Alan Raby

Hi,
You need to add a reference to the COm component Microsoft Excel 9.0
Object Library.That should solve the problem.
Hope that helps.
Regards,
Anis.
 
Hi Motty

If all what you wanna do is start a separate instance of excel passing it a
file then you can use the Process.Start( "path to excel file" );

Take a look at Process.Start( string ) in the MSDN.

Cheers,
 
Back
Top