What are some resources that will help get me up and running quickly with the Excel interop in C#?
7 回答
Ken Getz的文章从 Visual Studio 2005 开发人员的角度理解 Excel对象模型详细介绍了 Excel 对象模型。代码示例在 VB.NET 和 C# 中都有。
C# 和 VBA中涵盖了一些重要的警告: Like Oil and Water,也由 Ken Getz 编写。
我还会看到文章:如何使用 Visual C# .NET 构建 Office COM 加载项。
HTH...
1) First thing first; Download the Office Interop Assemblies from which you will access all of the objects, properties and methods in Excel interop and the appropriate references to your project. BE AWARE: Any machine that you intend to run your code on will need these assemblies installed also. You can either include them in your install package, or they come with .NET framework 1.1, so if your clients have that installed, they will probably have the interop assemblies.
2) There is a wealth of knowledge on MSDN...pretty much all of the objects and methods you will be using will be well-documented there.
3) NOTE: One weird little thing about using Interop with C# is that you have to supply "missing" references for interop calls manually...i.e. When using the functions in VBA (if you are used to doing that) if the method calls for 3 arguments and the last two are optional, you can "leave them out" in VBA (i.e. MyMethod argumentOne) ... this does not work from .NET it is the one thing that confused me for a while when I started using the Interop assemblies; You have to manually create a missing object like this (example is from Word Interop, but same principals apply to Excel or any other office Interop package (and you also have to box some arguments and pass them by ref as below):
object missing = System.Reflection.Missing.Value;
string somestring = "string";
object refstring = (object)s;
wrd.Selection.Hyperlinks.Add(wrd.Selection.Range, **ref refstring, ref missing, ref missing, ref missing, ref missing**);
I hope that helps.
If you are looking to learn Excel Object Model, the VBA help is pretty good.
You might use "Primary Interop Assemblies" to work with Excel
Here's a decent article with some useful examples.
MSDN is always worth a read.
通过录制宏(在 Excel 中)并分析所发生的事情,您可以学到很多东西。
我的主要编程语言是 C#,但为了与 Excel(或其他 Office/COM 对象)通信,我总是使用 VB.Net。这使得从 VBA(记录)传输到 .Net 变得更加容易。
VB.Net 使后期绑定更容易使用。首先我使用早期绑定(让我获得 Intellisense),然后我将类型更改为 Object,因此我不再依赖特定版本的 Office。
MSDN has a wealth of information...
http://msdn.microsoft.com/en-us/library/ms173186(VS.80).aspx