What's currently the best way to extend Excel using C#? - c#

I have the professional versions of VS2008 & VS2010.
I wish to add a couple buttons to a toolbar in Excel. When they are clicked I'd like to be able to open a form (either WinForms or WPF is fine) collect a few values from the user in the form and then take that data + read cell values from the current worksheet to perform some database operations.
What's currently the best way to do this using C#? I'd greatly appreciate a pointer to any examples / tutorials. My understanding is that VS2010 has improved the process alot but I may have to deal w/ Excel 2003 which I don't think it supports.
I get confused between Visual Studio 2008s Extensibility->Shared Addin template and other Office Addin templates I've seen. I'm not sure when which type of solution is appropriate.
I'm new to Office development so I'd really appreciate any help to get me going on the right track.
Thanks much.

A couple of starting points:
Walkthrough: Creating Your First Application-Level Add-in for Excel
How to build an Office COM add-in by using Visual C# .NET
Tips and Tricks: Building Microsoft Office Add-ins with Visual C# .NET
Also, you want to explore
.NET Excel Wrapper
.NET Excel wrapper was born out of
frustration from working with .NET and
Excel. The documentation for .NET
Interop.Excel is very scarce and the
API is bloated with hard to understand
parameters and methods that throw
unknown errors.
What .NET Excel Wrapper aims to do is
take the complexity out of working
with Excel in .NET and increase your
productivity, improve code readability
and reduce the amount of code you need
to write.

The default approach to extend office using .Net is VSTO. VSTO does support Excel/Office 2003; if you chose an Office 2003 project in Visual Studio, it will support 2003 and above, but by supporting 2003 you will be giving up some features which you can use if you target Office 2007 and above - like the ribbon or custom task panes.
Besides the links mentioned by KMan, I have a series of posts on my blog which provide a walkthrough for creating a Excel 2007 add-in with VSTO, which you might find useful.

Related

Excel Add In development,Compatibility of office 2010 and office 2013

I have used Shared Add In(C#) to develop an Excel Add-In. In the initialization, I had no problem with loading excel-dna file .xll on Office 2010. However, there is problem with running on either Office 2013 or 2016, showing that "function is Invalid".
AddIn udfAddIn = applicationObject.AddIns.Add(progID, true);//the exception is "AddIns is Invalid"
udfAddIn.Installed = load;
Please help me. I would like to know how it (.xll file) works on Office 2013 and 2016 by shared add in. Thanks!
You are mixing two different technologies when you make a Shared add-in (a COM add-in) with Excel-DNA. I'm surprised it worked with Excel 2010.
The best approach is to make a new Class Library project (targeting .Net Framework) and then install the ExcelDna.AddIn package from NuGet. Then follow the instructions in the readme file that pops up. You should have a starter add-in that works in both Excel versions.
The best place for Excel-DNA support is the Google group at https://groups.google.com/forum/m/#!forum/exceldna

How to make a quick analysis-like floating menu in VSTO

I want to know anybody knows if there is in VSTO a control or a way to create a popup like Quick Analysis that appears at the right-bottom of a given range:
I'm creating an Excel plugin in VS2013 with C#, the plugin should be compatible on Excel 2007, 2010 and 2013. If such control can only be created on the latest version of Excel (2013) that may not be an option for deploy. I´ve already created a traditional Windows form to do this, but I'll like to know if there are better ways to show new controls to the user, closer to where the action is occurring. Thanks!

Excel parsing C# without interop

What is best way and practics parsing excel files without using COM interop or OLEDB connection
You could use NetOffice, im currently using it in my project. Its pretty cool,
Code Sample for Excel
Features
Office integration without version limitations
All features of the Office versions 2000, 2002, 2003, 2007, 2010, 2013 are included
Active support in version independent development (please scroll down)
Syntactically and semantically identical to the Microsoft Interop Assemblies
No training if you already know the Office object model, use your existing PIA code
Reduced and more readable code with automatic management of COM proxies
Usable with .NET version 2.0 or higher
Easy Addin Development
No deployment hurdles, no registration
No dependencies, no interop assemblies, no need for VSTO
Visual Studio Project Templates and Wizards available Photo Gallery
I have had good success with ClosedXML.

VSTO Excel Add-In with Visual Studio Express

I want to create an add-in (a ribbon precisely) for MS Excel by using Visual Studio C# 2010 Express. I have searched and read about it here and here and somewhere else. However, when I want to create a new project I don't see any template as described, I don't see even the Office tab. Is it because I am using the Express edition? I think it shouldn't be. Because in this official comparison they say all editions are capable of office development.
What can I do to develop this add-in?
According to this post on the MSDN forum, Visual Studio Tools for Office are not available in the Express edition. I'm not sure where you're reading that on the comparison page (a quick glance said nothing about the Express edition).
A good alternative to VSTO for making an Excel add-in with .NET is Excel-DNA (which I develop). It is a free library that integrates your .NET assembly with Excel using the C API (as an .xll add-in). You can create user-defined worksheet functions (UDFs) for Excel (which are not supported by VSTO), and make ribbons, RTD servers. And you get easy deployment (a single .xll file) without needing any registration or admin rights, even for ribbons and RTD servers.
Another option, if you just want to integrate through the COM interface and not make UDFs or use the specialized Excel features, is NetOffice. The project give you a version-independent set of interop assemblies for Office and Visio, and allows you to easily make add-ins targeting different versions of Office. NetOffice can also be used as your COM interop library in an Excel-DNA add-in.
Both of these directions will work fine under the Express editions of Visual Studio, as well as in the free SharpDevelop IDE.

Do I lose the benefits of macro recording if I develop Excel apps in Visual Studio?

I've written lots of Excel macros in the past using the following development process:
Record a macro.
Open the VBA editor.
Edit the macro.
I'm now experimenting with a Visual Studio 2008 "Excel 2007 Add-In" project (C#), and I'm wondering if I will have to give up this development process.
Questions:
I know I can still record macros using Excel, but is there any way to access the resulting code in Visual Studio? Or do I just have to copy and paste then C#-ize it?
What happens with my "Personal Macro Workbook"? Can I use the macros I have stored in there within C#? Or is there some way to convert them to C#?
If there is some support for opening and editing VBA macros in Visual Studio, can you provide a very brief summary of how it works or point me to a good reference?
Do you have any other tips for transitioning from writing macros in VBA using Excel's built-in editor to writing them in C# with Visual Studio?
You are in for a world of hurt, if you want to port VBA macros to C#. If they are complex at all, I would just rewrite them in Visual Studio or keep them in Excel.
Also, as much as it pains me to say this, VB.net is really a better choice to do office development in at the moment. VB.net supports missing parameters which will come in handy for the Excel methods with a zillion parameters. This support will be added to C# for .NET 4.0.
You should be able to call a VBA method from .NET. Take a look at this:
http://msdn.microsoft.com/en-us/library/bb608609.aspx
You can also look at this article on codeproject: http://www.codeproject.com/KB/office/extending_excel.aspx
(Disclaimer: I develop the Excel-DNA library.)
You should have a look at Excel-DNA - http://exceldna.codeplex.com. The library allows managed assemblies written in C#, VB.NET or F# to expose high-performance user-defined functions (UDFs) and macros to Excel through Excel's native .xll interface. The project is open-source and freely allows commercial use.
With your VBA background, it might make sense to move to .NET using VB.NET first. Once you've made this shift, starting to use C# will be much easier and you can cross-reference assemblies written in VB.NET and C# with no problems.
Some add-in tools like ReSharper, and the free .NET IDE SharpDevelop, can even convert code from VB.NET to C# automatically. Certainly for using the COM automation interface from C# you want to use .NET 4.
Mike Staunton has a video of a talk discussion the VBA -> VB.NET via Excel-DNA technique here: http://www.thalesians.com/finance/index.php/Events/Seminars/Seminar27#Video.

Categories