Creating add-in for Excel using C# - c#

I want to use C# class methods in Excel. Can anybody guid me how to do it ?
The C# component will be excel add-in. How to create setup for this addin, so that I just need to give setup to user which will install add-in at client's machine. User should not need to do any other steps like registering the C# dll.

(Disclaimer: I develop the Excel-Dna library.)
You should have a look at Excel-Dna - http://excel-dna.net. 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 the native .xll interface. The project is open-source and freely allows commercial use.
With Excel-Dna you can create a single .xll add-in file that the user can open as an add-in without any further installation or registration. Excel-Dna add-ins can expose RTD servers and customized Ribbons for Excel 2007 and Excel 2010 without additional registration, so you need no extra setup program.

You will need to create a new Visual Studio project of type Excel 2007 Add-In (or Excel 2003 Add-In). The option for this can be found (in Visual Studio 2008) at:
New Project dialog -> Project types -> Visual C# -> Office
This Add-In will need to be installed on the target machine(s).
The Add-In will be able to hook into the Excel object model and therefore access any loaded spreadsheets etc.
The Add-In will also be able to add buttons to the Excel toolbar/ribbon.

Try looking at VSTO, there's a section explicitly for Excel

You can create Excel projects within Visual Studio (previously known as Excel VSTO projects) or you can use a third party library such as ExcelDna.

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

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.

Exporting DataGridView data to Excel file in C#

I need to export data from DataGridView (in my WinForms application) to excel file. I googled some tutorials on the subject, but I'm having hard time getting any of the available solutions to work.
I started with this tutorial. But when I try to export datagridview to excel file i get Unhandled COMException in:
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new
Microsoft.Office.Interop.Excel.ApplicationClass();
I've also tried this tutorial, but with the same result.
Another of the articles I've found and read was this one. Couldn't get code to work, probably because I can't find Microsoft Excel 12.0 object library in the COM tab of Add Reference dialog.
I'm using Visual Studio 2010 Ultimate.
If those libraries are not installed, you can download them from Microsoft:
For Specific office versions, see here:
Office 2003 Update: Redistributable Primary Interop Assemblies
2007 Microsoft Office System Update: Redistributable Primary Interop Assemblies
Microsoft Office 2010: Primary Interop Assemblies Redistributable
Once you install the correct one, you should be able to find the library in the 'Add reference' section of the tutorials
One day I did what you need but I got it in VB.Net
So you just need to create a Class Library project and in that class
use my method so you can reference it through C#.
Take a look at this link (translate it to English):
http://foro.elhacker.net/net/aporte_exportar_datagridview_a_excel_pdf_y_html-t299075.0.html
and also you can download my example from here and re-use my method:
http://dl.dropbox.com/u/15208254/stackoverflow/ExportarData.rar
Regards.
You can use Excel Package Plus and export your data without a need for excel, or any other external dependancy

Using excel in c#

I'm running Visual Studio 2010 Express, and in C# I'm trying to display Excel workbook.
I'm trying to use
Microsoft.Office.Interop.Excel
I have office 2007. Why doesn't this show up when I go to add> reference and navigate to the .NET or COM tabs?
Why doesn't this show up?
How can I add this as a reference or is there a better to show a workbook to the user in Excel?
For whatever reason VS 2010 doesn't like to show it in the .NET tab of the Add Reference window, you have to manually browse for it. The path to the assembly is C:\Windows\assembly\Microsoft.Office.Interop.Excel.
Go to Add Reference
Click on the Browse tab
Browse to C:\windows\assembly
Choose Microsoft.Office.Interop.Excel
I've had decent luck using EPPlus. It was awhile ago but I chose it since at the time the interop library was specific to a single version of Office whereas epplus was compatible with any version supporting the Open Office Xml format

How does Excel VSTO Work?

How does Excel VSTO Work? If I create an Excel Workbook solution in Visual Studio 2005 I can then happily code away with full access to the Excel object model and even treat the Excel sheet as a design surface. When I build the solution I get a .XLS file and a .DLL (containing my C# code).
I can now start up the Excel sheet just by double clicking on the .XLS and there is my sheet functioning with all my C# code and any controls I dropped on the sheet etc.
How is the sheet referencing the .DLL? What part of the excel workbook/sheet tells it that it needs to fire up the CLR and host my assembly?
According to this (thanks PintSizedCat) for Excel 2003 the following happens:
The Microsoft Office application
checks the custom document properties
to see whether there are managed code
extensions associated with the
document. For more information, see
Custom Document Properties Overview.
If there are managed code extensions,
the application loads AddinLoader.dll.
This is an unmanaged DLL that is the
loader component for the Visual Studio
2005 Tools for Office Second Edition
runtime. For more information, see
Visual Studio Tools for Office Runtime
Overview.
AddinLoader.dll loads the .NET
Framework and starts the managed
portion of the Visual Studio Tools for
Office runtime.
The Visual Studio Tools for Office
runtime creates an application domain,
sets policy for the application domain
not to trust the My Computer Zone, and
checks the code access security policy
store to find a policy for the
customization assembly.
The .NET Framework validates the
evidence presented by the assembly
against the policy. If it fails, an
error is raised. If it passes, the
process continues.
If the customization uses a deployment
manifest, the Visual Studio Tools for
Office runtime uses it to check for
assembly updates. If any updates are
necessary, they are performed now.
The Visual Studio Tools for Office
runtime loads the assembly into the
application domain.
The Visual Studio Tools for Office
runtime calls the Startup event
handler in your customization
assembly. For more information, see
Visual Studio Tools for Office Project
Events.
In my test project's Excel workbook I have two custom properties:
_AssemblyName, value = *
_AssemblyLocation, value = {533b2c13-a125-418a-bfff-9546b0762807}
I suppose these are the properties which direct the VSTO runtime to my assembly.
This is all done in the Registry, you should be able to find the key in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Excel or your equivelant application. I've more experience with COM Addins which are registered somewhere else in the Registry as well. This key should have a LoadBehaviour item under it which is used to determine how the application is loaded (2 is load manually, 3 is load automatically on startup).
Do you have a Setup Project for your VSTO? Inside there you can see the Registry key that is set, but the Setup program will/should also Register the VSTO in GAC (though don't take my word for it as I'm a bit shakey with VSTO as I said).
Hope this helps, I shall try and find some more information for you.
Edit
You should try reading the following http://msdn.microsoft.com/en-us/library/bb386298.aspx which will give you an explanation of what the addin is. It's really just a wrapper around a COM host which is loaded from the Registry and the VSTO talks to that using some Interoparability code.
Also useful are http://msdn.microsoft.com/en-us/library/23cw517s.aspx (Getting Started with Visual Studio Tools for Office, don't knock it because it says Getting Started in, there's a lot of useful info in there) and http://msdn.microsoft.com/en-us/library/hy7c6z9k.aspx (Which is linked from the first and is an overview of VSTO Addins).

Categories