sending commands from excel file to a c# program - c#

I have a program that reads Data from Excel and write data to Excel file. I need to send commands from the Excel file to the c# program (e.g. capture the F9 from the excel and get it on the c# program)
I am using Microsoft.Office.Interop.Excel framework.
How can i do it?>

the excel application interface also provides events to some excel functions, e.g.
Microsoft.Office.Interop.Excel.Application:
//
// Summary:
// Occurs after any worksheet is recalculated or after any changed data is plotted
// on a chart.
event AppEvents_SheetCalculateEventHandler SheetCalculate;

You will probably have to call native Win32 methods to add a message hook looking for a character message in the Excel window.

oferyo,
Hello. You don't mention what sorts of volume of data you need to deal with? Or the type, either (questions like: does the stuff 'sent' to the destination Excel file includes graphs or just data?).
You don't mention which version of C# you are using but you could consider calling a web service from the VBA script embedded in or added to the originating Excel spreadsheet?
MSDN article on calling WebServices from within VBA
I can't recall how to plumb VBA code into specific events on the Excel spreadsheet (like the recalculate/press of F9) but I think that should be trivial.
If you are using C# 3.5 or later you can host a WCF service within the application ... but I would suggest describing the question better because it may be the VBA->WCF/WebService->Application solution might be seriously over-engineered?
Hope this helps you think about the options you have within VBA ...
regards,
Aidanapword

Related

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

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

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.

Writing to Excel using Microsoft.Jet.OLEDB.4.0 + Editing at the same time

I used the code described here to update an excel sheet using c#.
The problem is that the file gets locked and cannot be edited manually while it is being updated (unlike the example code, i am doing constant updates, realtime data feed)
Since it is a trading application, operators need to be able to add excel formulas while other cells are being updated.
I am new to this and open to switch to any other technology/architecture making this possible.
I am receiving a data feed and I just want to put in an excel file and update it in realtime while making it possible for sheets to be edited manually. I guess if the dll or add-in is called from within excel rather than oledb I would not have such a lock issue. So if there is any way to call my code from excel and not the other way round (not too painstaking if possible) I welcome any suggestion
EDIT : Solution
I finally opted for RTD Technology for Excel which is exactly what I was looking for.
MS Reference is 10 years old and VBA oriented but it gives the big guidelines and things did not change much since. I think I would not have made it through without Kenny Kerr's Artcile on the subject. Many thanks to him.
You will need to integrate into Excel itself... Excel offers an interface to achieve exactly what you describe... it is based on COM and called RTD (Realtime Data-Server) and gets called from Excel.
You have to build a COM component which adheres to the official spec...
Some usfeul links to get you started:
http://msdn.microsoft.com/en-us/library/aa140059%28v=office.10%29.aspx
http://msdn.microsoft.com/de-de/library/microsoft.office.interop.excel.rtd%28v=office.11%29.aspx
http://exceldna.codeplex.com/workitem/5902 (free library to ease the development)
http://www.add-in-express.com/docs/net-excel-rtd-servers.php (commercial library to ease the development)

Methods to compile an Excel spreadsheet into a .NET assembly?

I have a spreadsheet that I'd like to compile into a form that I could call from C#.
Naturally, I'd like to be able to change the inputs to the spreadsheet before reading the calculated result.
What is your recommended method?
UPDATE:
To clarify, I want to make an existing Excel spreadsheet available as a web service that is callable from .NET. I can't have a dependency on Excel, as its running on a web server.
UPDATE:
I used the answer below, and it worked like a charm. Now I can prototype a formula in Excel, then convert it straight into C# and compile it into an assembly.
This question is also covered under Reading Excel Files as a Server Process.
FlexCel API Mate within TMS Flexcel Studio for .NET lets you convert an existing Excel spreadsheet into C# code, recalculate the spreadsheet, and read the result out of a cell using an API call.
See the video tutorial of FlexCel ApiMate. The video states, quote:
ApiMate will convert an Excel file
into a C#, VB.NET or Delphi.NET
program.
The docs also state:
Recalculation of more than 200 Excel
functions.
and:
You can add your own functions on the
code to the already big list
implemented by FlexCel, and use them
as native functions in your report.
UPDATE
Here is clarification from TMS tech support:
Emailed question:
I'd like to do the following:
Convert an existing .xlsx file to C# code, importing data from a database.
Allow FlexCel to recalculate the spreadsheet for me.
Read an answer out of a cell (for use elsewhere in my C# code).
Skip the step of writing the finished .xlsx file to the disk (we don't need this).
In short, I want to use FlexCel as an "Webserver Excel calculation engine", so we don't have to have Excel installed on the web server to perform spreadsheet calculations.
Are the steps I've described possible? Or have I misunderstood how the component works?
Emailed reply:
You can either load the file directly from the database (by opening from a stream) or use the APIMate tool (incuded in the tools folder) to convert the file to c# code.
Yes, FlexCel will recalculate it with XlsFile.Recalc()
Yes, you can read the recalculated values too.
Yes, you don't need to write the answer if you don't want to.
Besides this, for using it as recalculation engine, we have the "RecalculateCell()" method that won't recalculate the full spreadsheet, but only the cells needed to get the value in an specific cell. So, if for example your result is in A1, you can call RecalcCell in A1, and it will recalculate only all cells needed to get the value in A1 (including dependecies, so if A1 has a formula with a2, and a2 with a3, all 3 will be calculated).
There is also a RecalcExpression method, that will recalculate the value of any formula without needing to write it into a cell. So imagine you have a column of numbers at col A, and you want to know the sum. You could use RecalcExpression("=sum(A:A)"); to know the sum, without needing to enter a formula in B1 with the sum and then reading the value of that formula (which you could also do of course)
From Microsoft, there appears to be a framework called Excel Services "Develop A Calculation Engine For Your Apps"
Teaser excerpt:
This article discusses:
Excel as a server-based application
The Excel Services architecture and APIs
Creating managed user-defined functions
Building custom solutions with Excel Services
I have never used it, but the info-graphics on the main page are most encouraging.
Thanks for asking this :)
Calc4web converts spreadsheets into C++ code, which can be called from C#, Java, etc.
Quote from website:
Calc4Web gives programmers a better
way to get their job done: create a
small spreadsheet which holds the
logic, and push a button to turn that
spreadsheet into C++ code that works
on the first try, code that can be
called from any language: C,C++, C#,
Visual Basic, Java, and any other
language which can call into DLLs.
I also suspect that since it compiles the spreadsheet logic to purely native C++ code, it will be very fast compared to Excel (the website states "5,000 times faster").
Check out ActiveMesa X2C, a tool for converting Excel spreadsheets into C# and C++. (Disclaimer: I'm the author.)
For a list of Spreadsheet components that allow you to work with Excel spreadsheets without having a dependency on Excel, see DevDirect Spreadsheet Components.
SyncFusion Essential Calculate.
Quote:
... you can fully load,
manipulate, and compute Excel
spreadsheets with no dependence on
Excel.
There is no way to compile an excel spreadsheet into c# code.They are not in any way the same "thing", In the same way you can't compile this text that you are reading, or a word document into code or an assembly of some kind.
You have to be more specific with the kind of functionality you want to get, which will help you get an answer.
There are several ways (pointed out in other answers here) to allow you to access Excel spreadsheet data and utilize them, but this is in no way compiling them.

Getting real time values from an excel feed

Hello
My application from the stockbroker has this button called "Start excelfeed" which opens an excel file and then updates the incoming prices in realtime. I want these prices extracted into my application (Java), but after several tries with jxl and poi ive found out it only extracts the values which occurred last time i saved the excel files. Are there any possible ways to extract these prices in real time? I'm not sure which method theyre using to feed the excel file, except their app is written in C#.
You can have a look at the VBA code behind the "Start excelfeed" button by hitting Alt+F11 ... Then you can check how the excel macro taps into the data source und try to adapt that in Java
[EDIT]
#Zico Sorry then I got you wrong. In that case I'd try to automate it via the Java Robot Class like user489041 suggested or you could fire up wireshark and try to snoop around the network traffic
They are using the DDE technique and you should use excel automation to pull data from excel into your app.
In the old days, we used DDE links. Chances are your stockbroker addin uses precisely that.
Chances of using DDE from java are pretty slim (close to zero) because it is windows specific. However, possibly there are other methods involved (like COM components or TCP/IP connection). I'd suggest you get the values from teh stockbroker source like the Excelfeed, instead of going through Excel.
Of course, the problem with that is that you won't get help from the vendor of the Excel addin. If things are like they were 8 years ago, they are making tons on your buying the Excel addin and don't really want to help you program against that :)
Worst case, you could use the Java Robot class to create the Excel spreadsheet, save the Excel spreadsheet as a CSV file, then read the CSV file from your Java program.
You can do this with solutions like Obba. Obba allows to access a Java virtual machine running the "Obba Server" directly from Excel.
For your problem, you have to "create" your application from the spreadsheet (load the jar, create an object representing you app - e.g. launching it in a separate thread). Then you can feed the app from Excel... - In this case, Excel is to some extend the "control program" of your app. However, if you start the Obba Server process manually, the process will keep on running if you close and re-open Excel.

Categories

Resources