Excel 2003 Automation (connect to a running instance)

C

cybertof

Hello,

Is there a way to connect (through automation) a c# application to a
running Excel 2003 instance on a specific workbook ?

In the past, i used to use GetObject(...) function in VB6.

Regarding C#, I have heard about
System.Runtime.InteropServices.marshal.GetActiveObject
("Excel.Application")

but i think this is used only for COM Excel objects, isn't it ?

What about connecting to Excel 2003 ?
Is it COM interface or native interface (.net ? managed automation ? i
don't know what would be the name of the newest automation method)


Thanks for your help,

Regards,
Cybertof.
 
N

Nermin Dibek

I have previously connected to MS Office components (Excel mostly) from C#.
For both versions, the only way is to use the COM Interop (There are MS
Office Interop dlls available on Microsoft Web site, or you can add
Reference manually to the Office COM component and Interop wrapper is built
automativally).

Following is then sample C# code that I used to instantiate Excel App,
Workbooks, and Worksheets:

////////////////////////////////////////////////////////////////////////////
////////////

using Microsoft.Office.Interop.Excel;

//...

//This is comparable to CoCreateInstance

Application app = new Application();

app.Visible=true;


Workbook wkb =
app.Workbooks.Open("spreadsheetPath",Missing.Value,Missing.Value,Missing.Val
ue,Missing.Value,Missing.Value,

Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missin
g.Value,Missing.Value,Missing.Value,Missing.Value);



//Main processing of data

foreach(_Worksheet wks in wkb.Worksheets){

//Process all worksheets

Range wksRange;

try{

wksRange=wks.UsedRange;

builder.ProcessWorksheet(wks, ref wksRange, ref strm);

Console.WriteLine(wks.Name + " processed ...");

}

catch{

}

}

////////////////////////////////////////////////////////////////////////////
////////////

If you are only inetrested in Excel files, not manipulating all of the
features,may I recommend this great control:

http://www.codeproject.com/dotnet/ExportExcelData.asp?target=Excel|Export

It allows you to export excel spreadsheet from file directly into dataset.
No COM components required It is free with source code. It uses OleDb to get
the data.

Hope that helps.

Nermin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top