пятница, 4 февраля 2011 г.

C# code snipped: Create Excel instance using the dynamic keyword

The code below creates dynamic Excel.Application object without referencing the Excel COM server dll.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


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

dynamic excel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
excel.Visible = true;
Console.Read();

}
}
}

2 коммент.:

Tigor комментирует...

but the excel app gets closed right after my program.
how can i leave excel opened after my program has exited?

DmytroZ комментирует...

Hey Tigor,

I haven't tested that, but in order to connect to the existing Excel workbook you would need to use:
Marshal.GetActiveObject("Excel.Application");

Check out those threads for more info:

http://stackoverflow.com/questions/7646498/how-to-iterate-through-instance-of-excel-c-sharp/7646742#7646742

http://stackoverflow.com/questions/1340176/how-to-use-getactiveobjectexcel-application


Отправить комментарий

 

.NET ate my MOSK;. Powered By Blogger © 2009 Bombeli | Theme Design: ooruc