Excel DNA and Excel Add In COM - c#

I have had previous experience working with an Excel Add-In vsto COM object. Specifically using the Excel.Interop dll to create a worksheet and populate it with data. As well as, interact with the worksheet by using the worksheet change events to update data, thru c#. I wanted to explore Excel DNA and possibly use it. I am completely new to excel dna, but while researching it I only found examples of creating functions and a ribbon with buttons, but I haven't seen a way to create a worksheet and populate it from a datatable.
Is Excel DNA only used for the creation of the excel add in and if I wanted to create a worksheet, populate it with data and have events I either need to use excel.interop dll or OpenXML?

Excel-DNA is meant to allow you to run .NET code from within Excel. If you want, for example, to have a custom Ribbon in Excel, with a button that, when clicked, will run some .NET code to create a new Workbook and populate some data, etc. then yes... Excel-DNA is a great tool for that.
If you want to create Excel files outside of Excel, for example, in a Console App or Windows Service, then Excel-DNA is not the right tool for that, and you should look at using the Excel.Interop if you know your app will run on a machine with Excel installed, or other alternatives such as ClosedXml and other OpenXml-compatible tools, that will generate Excel files without requiring Excel installed on the machine.

You have full access to the Excel COM Object Model from within your Excel-DNA add-in. One important step is that you have to get hold of the correct Application root object for the Excel instance that is hosting your add-in. (Just calling new Application() might get hold of another Excel instance.) To get hold of the Application object you call ExcelDnaUtil.Application - that return the COM object.
From there you can use the dynamic support in C# to talk to the object model. But better is to reference the Interop assemblies, giving you IntelliSense and early-binding.
A convenient way of referencing a set of interop assemblies (corresponding to the Excel 2010 object model) is to install the ExcelDna.Interop package from NuGet. With the 'Embed Interop Types' feature in .NET 4 (which is set true by default), you need not redistribute anything special and your code will be compatible with all Excel versions, as long as the object model parts you use are supported there.
As an entry point into running the COM code, you can make a macro, shortcut ribbon or context menu. From the object model you could also hook COM events.
A simple example with detailed instructions for making a Ribbon button that then runs some COM code is available on GitHub.
Note that the VSTO wrappers on top of the COM object model (everything in a Microsoft.Office.Tools.Excel namespace) are not compatible with your Excel-DNA add-in, so you'd have to implement that functionality yourself based on the native COM object model (the types in the Microsoft.Office.Interop.Excel namespace.)

Related

Incorporate an Excel workbook in a VSTO Excel Add-In

Am rewriting an Excel VBA Add-In as a VSTO Excel Add-In (and switching to C#).
In the VBA version I have worksheets available to store formulas etc., and generally manipulate the data (which will have been copied in from the active worksheet when the add-in was invoked).
With VSTO there is no equivalent .xlam workbook with worksheets to store such formulas.
So I'm trying to figure out how to store/access such a "helper" workbook. I do realize I could build the formulas in code, but storing them in spreadsheet(s) makes it much more maintainable.
I've tried embedding such a workbook but am struggling to access it and then use it like I could in VBA.
In traditional VBA code, your code is part of a workbook and hence you could use to "store" some config. In an add-in, there is no such thing. It is application level and hence no specific document is available.
a) You could store the formulas as XML or something and do what you want with them
b) It seems like what you want is not an add-in, but a document level customization. If you do that, your "code" is attached to a particular document. There you could potentially use a "template" worksheet or even customize the sheets, look and feel, formulas, add ranges, whatever you need.

ActiveWorkbook in NPOI

I want to edit the current active Excel-Worksheet in NPOI.
The Codesample below Shows what I want to do. Problem is I would need it in NPOI and so I am asking whether any of you can help me as I can't find anything.
Microsoft.Office.Interop.Excel.Application excel = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Microsoft.Office.Interop.Excel._Workbook wb = excel.ActiveWorkbook;
NPOI is not a wrapper around Office, it implements it's own access to the files. As such, it cannot be used to get the "Active" instance of Excel. It never directly interops with Excel in the first place. That's one of the features that makes it a powerful library, it doesn't depend on Office being installed and can therefore run on a server in an unattended way.
In case you want to interact directly with Excel, you need to use the Microsoft Primary Interop Assemblies, which will work just fine with the statements you referenced in your Question.

Run/drive Excel via own application

Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?
This is not really for testing but for data scrapping similarly to selenium where you can drive a browser via C#.
Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?
Yes, you can use COM Automation from C#. Create a C# project and add a reference in the COM section of the dialog. You should reference Microsoft Excel 14.0 Object Library. This is the Excel 2010 version and if you use another version you should reference the version you have installed on your machine.
You can then automate Excel in a similar way as you would do using VBA except you now are using C#. On MSDN you have Getting Started with VBA in Excel 2010 that among other things explain how you can use the macro recorder to create a VBA subroutine from actions you perform in Excel. You then have to translate the VBA into similar C# code.

Get the list of User Defined Functions in Excel using C#

I am writing an Excel Addin in VSTO using C#, with some UDFs. There are other addins installed to the workbook which expose their own UDFs. I want to find out the list of all User Defined Functions and not the ones that come along with Excel. Is there any way I could do that?

Excel 2007 Addin - Technology options

We have a number of .xla/.xlam Excel Addins and the time has come to migrate to something easier to version control and maintain.
I'd like to write in C# if possible and the Addins will need to do the following sorts of things:-
Provide User Defined Functions to Excel
Create and manipulate named ranges in the Excel sheet
Pull data from external sources and populate cells in the Excel sheet
Currently all this is possible and simple to do with a .xlam what are the pros/cons of moving to VSTO or creating a C# Addin (I notice VS2010 has a New Project option of creating an Excel 2007 Addin).
Are there any good sources of documentation?
Thanks
Dave
I would recommend you seriously look at Excel DNA (Free) or Addin Express (chargeable). Both provide .Net functions via the .XLL interface together with .COM to .NET interface if you need it.
Performance of both of these makes VSTO look like molasses, and installation is realtively simple.
For easy creation of performing UDFs XLDNA is VERY hard to beat.

Categories