Storing an Excel Add-In in an existing workbook - c#

I'm currently developing an Excel Add-In using the Excel-DNA library. Sadly I need to serialize the Add-In somehow into the workbook so if the workbook is opened, the Add-In's code may be executed.
Before, I worked with VBA and Excel macros which've been run greatly, I could serialize the macro and upload the workbook. A software put data into it and delivered the edited workbook and I just had to open the workbook for the macro to execute.
I tried that with an Add-In (e.g. loading the Add-In and saving the workbook) but that didn't work, the Add-In seems to be not saved at all. Is there any other chance in serializing the Add-In into a workbook so people who just got the workbook can execute the Add-In's code?

You can include code in the Excel workbook that will install the add-in if it isn't already. You could also have it uninstall the add-in on close if you only want the add-in installed while the workbook is open.
Check out JKP's Name Manager, specifically the code in the Setup Name Manager file.
http://www.jkp-ads.com/OfficeMarketPlaceNM-EN.asp
It finds the add-in, which is assumed to be in the same path and as the Setup file, and copies it to the UserLibrary, which is the default path for add-ins. Then it sets the Addin.Installed property to True.
JKP's addin is and native Excel add-in, not an Excel DNA one, so there may be some differences (and I don't know what they are). But you may have to deal with Application.COMAddins rather than Application.Addins.

The way I understand Add-Ins, they sit separately in a way they can't be bound together with a workbook. The way I have understood them (and used them up to this point) is, for lack of a better term, a weird 'mini-program' that sits loaded inside Excel itself, not inside your workbook - you install it, it has access to more PC resources, Add-Ins even land in the list of programs for Windows, unlike a VBA macro.
Add-Ins solve a lot of problems, but do create a new one in the process. Even if you built a process that reached out on launch for the addin to install, I think it would still require a restart of Excel to be accessible from within the application, which would be frustrating. Add-In deployment is still fairly clunky, in my opinion.

Where I work we have a deployment process to deliver a Start Menu entry that will open excel and the correct version of the xll.
I have never heard of people embedding the addin in the workbook. But you can do the opposite, your addin can be coupled with a XLA or you can create an Addin menu with entries for the user to open a specific version of the workbook. This tie the workbook to a specific version of the addin rather than the opposite like you asked but it may be useful for your purposes. Uou could use a network drive for a basic implementation.
This would achieve kind of the same result by inverting your logic. However this is a costly solution in terms of manpower to create this infrastructure.

Related

Writing on open Excel application using NPOI

folks,
Environment
Windows 8.1
Visual Studio 2013
C#
Issue
How do I write values and make charts on visible Excel sheets using NPOI (https://npoi.codeplex.com/).
Why do I want that?
I'm developing an application to measure temperature in an apparatus. To put together experimental data in one place, I'd like to record data on an Excel sheet and make a chart on the sheet. In addition, I'd like to keep the Excel sheet visible and check the chart updated in real time.
You could also make graphs on Windows Form apps with MeasurementStudio by NationalInstruments for example but considering the flexibility of Excel charts (size and xy range changeable, easy-to-use user interface, etc...), I'd like to stick to Excel.
You can easily do this with Microsoft.Office.Interop.Excel by
ExcelApp.visible = true;. However, this module requires users to release every COM object generated. Otherwise, the objects remain and eat up memory. This is the reason I prefer to use NPOI.
How can I achieve this? Any answers would be appreciated.
You cannot do this with NPOI. NPOI reads and writes data from serialized Excel files. You cannot access those files while Excel has them open, and even if you could, Excel simply wouldn't re-read the files so your modifications wouldn't show up.
The problem you describe comes down to "I want to interact with a running Excel instance without using Excel interop". That's not going to work.

Loading Libraries to Excel Through Windows Form C#

This is my first question and I'm horribly scared my answer could already be on some other post. But I've searched for hours and I really can't get my head around it.
So here's the thing.
I'm working on a windows form that, very simplistically, has to open certain workbooks and run macros inside these workbooks.
The macros in these workbooks use certain functions that do not belong to standard excel libraries. I work in a bank and we have some proprietary functions and add-ins which are loaded every time we open certain exe files.
I can launch an excel application through:
Microsoft.Office.Interop.Excel.Application xlApp =
new Microsoft.Office.Interop.Excel.Application();
but this calls the normal excel. How can I add the libraries I need in this specific instance of excel?
The alternative solution is to launch excel with the libraries manually, using
System.Diagnostics.Process.Start("CMD.exe","My fancy excel");
but this takes a huge amount of time, as a new instance has to be created for each workbook.
Any thoughts or clarifications needed?
Thank you all for your time, hope I was clear enough!
When starting Excel through automation, it won't load any add-ins. So you have to explicitly load the add-ins using:
Application.RegisterXLL (for .xll add-ins), or
AddIns.Add / AddIn.Installed = true (for .xla or .xlam add-ins).
Here is an older question on the topic: Loading addins when Excel is instantiated programmatically, and a related MSDN blog post: http://blogs.msdn.com/b/accelerating_things/archive/2010/09/16/loading-excel-add-ins-at-runtime.aspx and another Microsoft support article: https://support.microsoft.com/en-us/kb/213489.

Reading/Writing Excel sheet real time in C#

Is there any free or purchased library or any API which allows me to update excel sheet while it is opened also.
I'm not certain of the nature of your project, but perhaps you could create an Office Add-In? I've had some very good experiences with Add-In-Express: http://www.add-in-express.com/add-in-net/index.php
If just creating an add-in isn't going to work for your application, consider writing a plugin that your application can communicate with to do the work for you behind the scenes.
If you want your Excel spreadsheet to be updated in real time, you have to develop an RTD which can be queried by Excel itself. Please refers to Microsoft documentation for that. Add-In express provides you excellent simplified API to develop that, we are using them and we are very happy with that.
There is no tool that can do that since the file is locked by the operating system.
With COM you should be able to drive a workbook that has been opened by the user.

What is the best practice to create a Excel 2007 Workbook

I want to know what is the best practice to create a Excel 2007 Workbook using C#, with its datasource being a raw flat file or a table in database.
You can use
"Open XML SDK 2.0 for Microsoft Office"
It's more comfortable than harcore manually hacking OpenXML spec. There are .NET strongly typed wrapper classes so it's not hard to create a simple sheet. You don't need any interop and msoffice installed and it's safe for server soluitions - there are only a few dlls which you can ship in your solution.
I did mail-merge solution and it wasn't so scary.
But as always, when it's possible, I'm prefering plain csv format.
I personally like creating CSV's, which can be opened directly in Excel. It's a lot less work than trying to hack the Office Open XML specification, and you don't need COM interop to Excel (which requires a copy of Excel to work).
You can use the Office Primary Interop Assemblies to completely automate Excel 2007, and create the workbook from within C#.
This gives you the most control, as you have complete control over how you map from DB or flat file -> Excel workbook/worksheet.
I am using
http://www.leniel.net/2009/07/creating-excel-spreadsheets-xls-xlsx-c.html
for creating excel . Seems good so far.

Using templates in excel with C#

As part of the c# program that I am producing I need to generate 1 workbook containing; 2 different worksheets and a 3rd that could be produced any number of times, what is the most effective way of doing this? I have looked into using templates although I am unsure how to repeat certain worksheets whilst only displaying others once. Any help or advice would be appreciated.
A simple way is to make a hands off template example workbook with the three worksheets. Then make a copy of it. Open both and re-copy worksheet number 3 on to the working workbook as a new worksheet as needed.
In response to the comment:
There are a couple of excel engines in a .net component products our there like spreadsheet gear or aspose cells. But if your application is a windows form based and where the application is guaranteed to run has office you can use office automation. You can't legally use office automation on a web server, but it is just as possible on a web server as on a client desktop. I've used the aspose cells and it's very easy to work with and very capable and a little less expensive than spreadsheet gear, but spreadsheet gear does also have a good reputation. Both of those components have very good documentation on how to do anything with excel. But if you have excel and want to use office automation, be sure to look for example code on the web on how to properly close excel from c# or vb.net. There are some tricks to getting it to close properly.
SpreadsheetGear for .NET has ISheet.CopyAfter / CopyBefore methods which enable you to copy an entire worksheet within a workbook or between workbooks.
You can see an example of duplicating a single worksheet multiple times in the Worksheet with Chart to Multiple Worksheets with Charts sample on the SpreadsheetGear / Excel Reporting Samples page here.
Disclaimer: I own SpreadsheetGear LLC
I have done this before with templates. I would create a template xls with the first two worksheets that you don’t want changed, then add a 3rd worksheet that you could copy to the end of the workbook (as you need more worksheets).
If you know ahead of time how many of the 3rd worksheet you need, then you can copy them to the end and delete the template 3rd slot.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.CopyWorksheetToEnd(3);
t.RemoveAtIndexWorksheet(3);
t.SetSomeValue(3);
t.SetSomeValue(4);
t.Close();
If you don’t know, then keep the template around to copy it to the end as needed, then when you are done, just remove the 3rd worksheet template.
ExcelTemplateManager t = new ExcelTemplateManager(template_path, log_path);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(4);
t.CopyWorksheetToEnd(3);
t.SetSomeValue(5);
t.RemoveAtIndexWorksheet(3);
t.Close();
I used the Microsoft.Office.Interop.Excel dll to create my ExcelTemplateManger class. The basic idea is to create a copy of the template excel file, and work off the copy. Let me know if you need help setting that part up, but it should be too bad.

Categories

Resources