学习 c# Excel 互操作的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/175763/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:42:28  来源:igfitidea点击:

Resources for learning c# Excel interop

c#excelresourcesinterop

提问by yoyoyoyosef

What are some resources that will help get me up and running quickly with the Excel interop in C#?

有哪些资源可以帮助我快速启动并运行 C# 中的 Excel 互操作?

回答by Ben Hoffstein

Here's a decent articlewith some useful examples.

这是一篇不错的文章,其中包含一些有用的示例。

回答by theraccoonbear

回答by Ben Hoffstein

MSDNis always worth a read.

MSDN总是值得一读。

回答by shahkalpesh

If you are looking to learn Excel Object Model, the VBA help is pretty good.

如果您想学习 Excel 对象模型,VBA 帮助非常好。

You might use "Primary Interop Assemblies" to work with Excel

您可以使用“主要互操作程序集”来处理 Excel

回答by BKimmel

1) First thing first; Download the Office Interop Assembliesfrom 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 .NETit 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):

1) 第一件事;下载Office 互操作程序集,您可以从中访问 Excel 互操作程序中的所有对象、属性和方法以及对项目的适当引用。 请注意:您打算在其上运行代码的任何机器也需要安装这些程序集。您可以将它们包含在您的安装包中,或者它们随 .NET Framework 1.1 一起提供,因此如果您的客户端安装了该程序集,他们可能会拥有互操作程序集。

2) MSDN 上有丰富的知识……您将使用的几乎所有对象和方法都在那里有详细记录。

3) 注意:将 Interop 与 C# 一起使用的一件奇怪的小事是,您必须手动为 interop 调用提供“缺失”的引用……即在 VBA 中使用函数时(如果您习惯这样做),如果该方法调用 3 个参数和最后两个是可选的,您可以在 VBA 中“将它们排除在外”(即 MyMethod argumentOne)……这在 .NET 中不起作用,这是当我开始使用 Interop 程序集时让我困惑了一段时间的一件事;您必须像这样手动创建一个缺失的对象(示例来自 Word Interop,但相同的原则适用于 Excel 或任何其他 office Interop 包(并且您还必须将一些参数装箱并通过 ref 传递它们,如下所示):

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.

我希望这有帮助。

回答by Mike Rosenblum

The article Understanding the Excel Object Model from a Visual Studio 2005 Developer's Perspectiveby Ken Getz covers the Excel object model in detail. Code examples are in both VB.NET and C#.

Ken Getz撰写的文章从 Visual Studio 2005 开发人员的角度理解Excel 对象模型详细介绍了 Excel 对象模型。代码示例在 VB.NET 和 C# 中都有。

Some important caveats are covered in C# and VBA: Like Oil and Water, also by Ken Getz.

C# 和 VBA中涵盖了一些重要的警告: Like Oil and Water,同样由 Ken Getz 撰写。

I would also see the article: How to build an Office COM add-in by using Visual C# .NET.

我还会看到文章:How to build an Office COM add-in by using Visual C# .NET

HTH...

哈...

回答by GvS

You can learn a lot by recording a macro (in Excel) and analyzing what has happened.

通过记录宏(在 Excel 中)并分析发生的情况,您可以学到很多东西。

My primary programming language is C#, but for my communiction to Excel (or other Office/COM objects) I always use VB.Net. This makes it easier to transfer from VBA (recorded) to .Net.

我的主要编程语言是 C#,但为了与 Excel(或其他 Office/COM 对象)通信,我总是使用 VB.Net。这使得从 VBA(录制)传输到 .Net 变得更容易。

VB.Net makes its easier to work with late binding. First I use early binding (gets me the Intellisense), afterwards I change my types to Object, so I don't depend on the specific version of Office anymore.

VB.Net 使后期绑定更容易工作。首先我使用早期绑定(让我获得 Intellisense),然后我将我的类型更改为 Object,所以我不再依赖于特定版本的 Office。