Dynamic Keyword Example of Excel Automation In CSharp – DEMO2

Let me show you another great use of dynamic keyword for COM interop. If you done any automation of Microsoft office application in the past then probably you familiar of adding COM assembly in to your project. I am going to do this without adding the assembly using dynamic.

class Program
static void Main(string[] args)
Type excelType = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(excelType);
excel.Visible = true;
dynamic sheet = excel.ActiveSheet;
Process[] processes = Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
sheet.Cells[i + 1, "A"] = processes[i].ProcessName;
sheet.Cells[i + 1, "b"] = processes[i].Threads.Count;

It will open an excel file with all Process Name and Threads Count



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s