How do I run vba scripts embedded in a spreadsheet in .Net? - c#

We have a .Net/C# based web service.
Within the service, we use a spreadsheet as a calculationr engine.
We're using Apose.Cells for .Net to populate the spreadsheet cells.
Calculations are performed through VB script embedded in the spreadsheet.
To be able to generate results, I need to be able to run/trigger the VB script.
Aspose does not seem to support this functionality.
It can poke/retrieve data and force recalc of any formulae that exist in the spreadsheet, but not run macros/VB script.
Once I've uploaded the desired data into the spreadsheet, how do I run the macros through .Net?
Thanks,
JohnB

Aspose.Cells cannot run macros. Besides, we are afraid, there is no server-side solution for your problem too. However on client-side (local machine) you can execute macros using Microsoft Office APIs. Please use the namespace
i.e
using Excel = Microsoft.Office.Interop.Excel;
Here is an example how to make use of it.
https://social.msdn.microsoft.com/Forums/Lync/en-US/2e33b8e5-c9fd-42a1-8d67-3d61d2cedc1c/how-to-call-excel-macros-programmatically-in-c?forum=exceldev
Note: I am working as Developer Evangelist at Aspose

Related

MVC Excel Interop

I have an MVC app that uploads an XLS file and imports it. Ther main issue is that these will often be password protected.
Unfortunately we can't have Office on the server so interop is not an option server side.
I was thinking that when the user browses to the file via their browser and clicks import that I could some how transform the file into plain old CSV before it actually gets to the server at which point I could do the main import. It's an intranet app so the clients will have Office.
So the question is, is there anypoint between the users selecting the file annd clicking upload where the file will be available for me to process on the browser side (where interop would be available) to transform the file?
No. You can't interact with programs on the clients machine (except with browser addons like ActiveX, don't go there). And you should definitely NOT use Interop on the server.
I would suggest to use EPPlus. It can read/write Excel files, without actually the need of interop. It only works with XLSX documents, so I hope it suits you. Else you might need a third-party library that is capable of reading XLS files.
As per Patricks answer you can't interact with a program on a client machine. An alternative to EPPlus is NPOI which is similar in that it removes the need for interop however it works with either XLS or XLSX documents.
Ended up using Syncfusion which does the job nicely.

Generating an Excel File Example for Visual Studio 2010 and ASP.net and C#

I've come across ExcelPackage and I found a couple of examples of using it, but none seem to work, they've all got some aspect, component missing or are for a different version of Visual Studio. I simply need to generate a .xls or .xlsx or even a .csv file, but as I am using a 3rd party server I can't use the Office .com objects. I have used SpreadsheetGear in the past which is expensive and as I am retired, I can't afford this sort of product.
If anyone has a working example of ExcelPackage or any other freeware offering, or can point me in the direction of one that has everything I need, it would be appreciated. A regular Web App rather than MVC would be preferred.
Take a look at Simplexcel by Michael Stum. It is designed around simplicity, is fully supported under ASP.net and should allow you to make simple, but extremely usable Excel cheats. You have an simple example available here.
Check out the Open XML SDK. This gives you the ability to generate and manipulate Office documents without using Office itself or the interop, and as such makes it a suitable approach from the server-side.

I need a standalone .net dll to process openxml spreadsheet formulas realtime

I am writing an application that will live on a server I will not have access to.
My task is to build a web interface that will encapsulate some EXTENSIVE (200+ formulas) logic based in an Excel spreadsheet.
I will not be able to install any software on this server.
I do not want to recode all the formulas in C#.
What I would like is to use a standalone dll that will allow me to open an OpenXML formatted spreadsheet file, change the input cell values, and then extract the end values from cells containing formulas which have run on the new data.
Is there anything out there other than Excel interop (which requires Excel be installed) that will actually compute the formulas realtime?
Since you want to work with OpenXML files, you can use OpenXML SDK 2.0, which is available here: http://www.microsoft.com/download/en/details.aspx?id=5124. You should install it on your developer environment and it helps you to read and modify contents of OpenXML files in your .NET application.
I haven't tried to work with formulas using this SDK, but I suppose it's possible, however, it may require some work (like writing a parser) to recalculate the formulas with you program.
There is, however, a workaround for this, but I'm not sure if it's acceptable for you. You can make Excel reevaluate all the formulas itself when the file is opened by a user. You can find some information about it here: OpenXML SDK: Make Excel recalculate formula.
I also recommend you to use OpenXML SDK 2.0. Productivity Tool, which is a part of the SDK. It allows you to view the contents of an OpenXML file and even show you the c# code required to recreate it programmatically, so it can be really helpful.

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.

SqlBulkCopy to upload Excel

If Im using SqlBulkCopy in .net 4.0 on a sql 2008 R2 database to upload excel data to the database, do I actually need Excel on the box hosting the site for this to work?
The reason I ask is I have a site up and running with this, and the hosting box has excel. The customer wants the site moved to a new box, this one doesnt have Excel and that bulk copy doesnt work. Other than the lack of excel there is no other difference.
thanks
If you are using the Microsoft Excel Object Library (COM Interop) to interface with Excel from .Net 4, then yes you need Excel installed as that is interfacing with the Excel object library.
What are your project references in C#?
Anything link to office automatiion would need taht part of office that is being automated. Also, if .Net 4 that objects may be referenced with the dynamic keyword.
Hard to tell exactly without knowing the project, references, and relevant code.
However, there are several third party libraries that allow interaction with Excel without having Excel installed. So, if you need to work without Excel, investigate using those.

Categories

Resources