C# create excel sheet 后期绑定
C# create excel sheet late bound
使用winforms,C# FW4.5打开一个excel sheet with late bound,像这样:
objExcel = CreateObject("Excel.Application")
现在我想使用 InvokeMember
方法,但我不知道 excel 的所有成员都可以调用。
例如,我知道我可以这样调用它:InvokeMember("Close",...
以关闭 excel,但是我在哪里可以找到我可以调用的所有成员的列表以及每个成员的作用?
Late-bound
Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:
如果您必须使用 late-binding,使用 c# 4.0 的 dynamic
关键字比 InvokeMember
容易得多,尽管它不会告诉您可以调用哪些方法提前。
检查以下通过 dynamic
关键字使用 late-binding 的代码。请注意 Visual Studio 如何允许我输入任何旧内容。虽然最终成员的 auto-complete 不可用,但它确实显示了我已经使用过的项目的成员。直到运行时我才知道我是否做对了(这就是 late-binding 这种方式的限制)。
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
...更具体地说:
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
虽然后期绑定很好,但即使使用 dynamic
,我也喜欢早期绑定。要获得方法列表,通过将 Microsoft.Office.Interop.Excel 添加到您的项目来使用早期绑定要容易得多 type-safe。
早期绑定:
var application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = true;
application.ShowWindowsInTaskbar = true;
这是在 VS 中:
C# 4 优点
c# 4 带来了一些只有在处理 COM 时才会看到的东西,例如 索引属性 - 在 c# 类型中不可能的东西。
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
例如
ws.Range["A1", "B3"].Value = 123;
...比 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...
告诉我更多
C# 4.0 - New C# Features in the .NET Framework 4,MSDN Mag 2010 年 7 月
Dynamic .NET - Understanding the Dynamic Keyword in C# 4,MSDN Mag 2011 年 2 月
使用winforms,C# FW4.5打开一个excel sheet with late bound,像这样:
objExcel = CreateObject("Excel.Application")
现在我想使用 InvokeMember
方法,但我不知道 excel 的所有成员都可以调用。
例如,我知道我可以这样调用它:InvokeMember("Close",...
以关闭 excel,但是我在哪里可以找到我可以调用的所有成员的列表以及每个成员的作用?
Late-bound
Using winforms, C# FW4.5 to open an excel sheet with late bound, like this:
如果您必须使用 late-binding,使用 c# 4.0 的 dynamic
关键字比 InvokeMember
容易得多,尽管它不会告诉您可以调用哪些方法提前。
检查以下通过 dynamic
关键字使用 late-binding 的代码。请注意 Visual Studio 如何允许我输入任何旧内容。虽然最终成员的 auto-complete 不可用,但它确实显示了我已经使用过的项目的成员。直到运行时我才知道我是否做对了(这就是 late-binding 这种方式的限制)。
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
...更具体地说:
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
虽然后期绑定很好,但即使使用 dynamic
,我也喜欢早期绑定。要获得方法列表,通过将 Microsoft.Office.Interop.Excel 添加到您的项目来使用早期绑定要容易得多 type-safe。
早期绑定:
var application = new Microsoft.Office.Interop.Excel.Application();
application.Visible = true;
application.ShowWindowsInTaskbar = true;
这是在 VS 中:
C# 4 优点
c# 4 带来了一些只有在处理 COM 时才会看到的东西,例如 索引属性 - 在 c# 类型中不可能的东西。
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
例如
ws.Range["A1", "B3"].Value = 123;
...比 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...
告诉我更多
C# 4.0 - New C# Features in the .NET Framework 4,MSDN Mag 2010 年 7 月
Dynamic .NET - Understanding the Dynamic Keyword in C# 4,MSDN Mag 2011 年 2 月