Resources for learning c# Excel interop [closed]

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


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

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

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

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


MSDN is always worth a read.


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

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.

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.