C# program runs too long mapping to an excel sheet - c#

I have a C# program that takes a legacy report file and maps to an Excel. It was running ok but we changed the process. The legacy program groups all the detail rows together and my program breaks page rather than before when they provided all the pages. this had added to run time about 4 times as long.
I have been told that if I can manually modify Excel to create 'proper' output for input file, It can speed things up a good bit.
also go to an Excel code-behind or add-in which would run from Dxcel and thus be faster
Can someone direct me how to apply these 2 ideas?
We do the code now as an array and write the entire row rather than cell by cell.
here is copy of the code: http://www.mediafire.com/?cebg17u5wl0ir25

Automation of Office applications is generally very slow. I just encountered this problem while trying to create a complicated graphic with Visio form c# code. It lasted about 30s. Now i create a SVG-File, that is then opened in Visio. Creating the SVG-File lasts less than 1s now!
I suggest that you export your data as CSV-File and then import it into Excel. Do only the minimum, i.e. the creation of worksheets, the import of the CSV and the formatting, with Excel-automation.

Related

copy in a single spreadsheet tables from different spreadsheets

I have different excel spreadsheets that contain tables with the same structure and the same header.
I need to conserve the header in the first spreadsheet and copy the tables of the other spreadsheets one beside the other in the first one, without repeating the header every time.
I have 150 or more spreadsheets, it takes a long time to do it by hand.
How can I solve this problem with a programming language like Python, C# or excel VBA?
I saw similar questions but I didn't manage to solve this problem with the answers given to those questions.
Question 1
Question 2
I would probably automate the process using whichever programming language you are most comfortable with that also has a pre-made Excel package. For instance, in C#, I used to use EPPlus.
Essentially you would write code that opens the existing first spreadsheet for writing, opens the other spreadsheets one by one and reads the table content starting from row 2, and copies the data into your master one. Rinse and repeat.
I would even clone the original spreadsheet and rename it to something else while you are testing this so that you don't accidentally clobber anything.

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.

Need to automate crunching of excel data?

I have processes running on Windows XP/7. They generate weekly .csv data files. I have a bunch of excel formulas that crunch the numbers for each .csv file produced for the week separately and then when adding the weekly data to the one big spreadsheet containing all the data put together.
The number of rows varies each week and for each process. So I can't hardcode that number in my dozens of formulas. So right now I go through this stupid process of manually entering the formulas each week into the .csv files.
There's got to be a way of automating this. Just now I quickly looked into doing this through C# or VB code. Could somebody recommend the best way to do this. Is C# or VB the right way to go? If so, any hints on how to put it all together - what's the model to use? For example, would it look something like this:
C# module reads in .csv data file
C# module creates an Excel spreadsheet and populates it with the .csv data
C# module runs my formulas on the all the rows.
Is that how one would approach it? Is there a better way for somebody who has very limited knowledge of C# or VB? I know Java and C++.
Any advice would be highly appreciated.
Thanks
From your explanations in comments, it appears that having a series of template Excel sheets would greatly facilitate the task.
So, for each process that generates data, you say the formulas are always the same, meaning that the columns are always the same (am I right?).
So, even if you don't know how many rows of data, you can still either create a template where only the first row is filled with formulas, and then you simply copy that row over and over, filling it with data as needed, or, you could fill a relatively "comfortable" number of rows with those same formulas, and fill in the data.
There are tons of atricles on how to Interop with Excel, so it's beyond my intent to provide you with specific code, but the idea is good.
If I can allow myself, I have worked in the past with a very interesting tool call Flexcel Studio for .NET, and I have found it to be of great help when it came to generating Excel sheets based on such templates.
Cheers
As others have suggested, I would recommend performing the calculations outside of excel if possible. There are plenty of stats libraries out there that are friendlier to work with than going through the hassle of moving data into excel, applying formulas to cell ranges, and so on.
If you really want to go the excel route, you can either use open-source libraries such as EPPLUS (.NET) or POI (Java) to work with .XLSX files directly. Some libraries do not support function evaluation so you will need to consider this when deciding on a library to use.
If you go with COM interop, you should read about about the following: Considerations for server-side Automation of Office.
As for the C# or VB (if not java with POI), I would go with C#. C# syntax is similar to java.
There might be a really simple solution to this problem.
Add 1 piece of auxiliary data to the .csv file either programmatically when running my process or when creating the .xlsx file (with all the formulas) from the .csv file. The auxiliary piece of data is the row count which will be in some known location.
Then modify all my formulas to use the INDIRECT function to specify the range using the cell
with the auxiliary piece of data.
I think that might work.

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