C# create excel sheet late bound

不想你离开。 提交于 2020-06-27 15:52:11

问题


Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:

objExcel = CreateObject("Excel.Application") 

Now I want to use the InvokeMember method, but I don't know all the members of excel I can invoke. For example, I know I can call it like this: InvokeMember("Close",... in order to close excel, but where can I find list of all the members I can invoke and what each one of them does?


回答1:


Late-bound

Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:

If you must use late-binding, using c# 4.0's dynamic keyword is a whole lot easier than InvokeMember, though it won't show you what methods you can invoke ahead of time.

Check out the following code that uses late-binding via the dynamic keyword. Notice how Visual Studio allows me to type in any old thing. Though auto-complete for the final members aren't available, it does show members for items I've used already. I won't know until runtime whether I got it right (such is the limitation of late-binding this way).

C# now supports dynamic late-binding. The language has always been strongly typed, and it continues to be so in version 4.0. Microsoft believes this makes C# easy to use, fast and suitable for all the work .NET programmers are putting it to. But there are times when you need to communicate with systems not based on .NET....The dynamic keyword in C# is a response to dealing with the hassles of these other approaches Tell me more

...and more specifically:

The COM interop scenario that the C# team specifically targeted in the C# 4 release was programming against Microsoft Office applications, such as Word and Excel. The intent was to make this task as easy and natural in C# as it always was in Visual Basic. Tell me more...

Early-bound

OP:

Now I want to use the InvokeMember method, but I don't know all the members of excel I can invoke

Though late binding is fine, even with dynamic, I like early binding. To get a list of methods, it's much easier and type-safe to use early binding via adding Microsoft.Office.Interop.Excel to your project.

Early binding:

var application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = true;
application.ShowWindowsInTaskbar = true;

Here it is in VS:

C# 4 Goodness

c# 4 brings with it some stuff you'll only see when dealing with COM, like indexed properties - something not possible in c# types.

You can’t define types in C# that have indexed properties, but you can use them provided you’re doing so on a COM type more

Some smaller language features in C# 4.0 are supported only when writing code against a COM interop API

e.g.

ws.Range["A1", "B3"].Value = 123; 

...is a whole lot easier than pre-c# 4:

ws.get_Range("A1", "B3").Value2 = 123;

C# 4.0 supports indexed properties on COM interop types. You can’t define types in C# that have indexed properties, but you can use them provided you’re doing so on a COM type more...

Tell me more

  • C# 4.0 - New C# Features in the .NET Framework 4, MSDN Mag July 2010

  • Dynamic .NET - Understanding the Dynamic Keyword in C# 4, MSDN Mag Feb 2011



来源:https://stackoverflow.com/questions/34195650/c-sharp-create-excel-sheet-late-bound

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!