Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

excel

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.

like image 516
Contango Avatar asked Apr 03 '11 20:04

Contango


People also ask

Can you compile an Excel spreadsheet?

It is quite easy to compile a workbook to EXE. In fact, the Excel compiler XLS Padlock compiles your Excel spreadsheet into an executable application (EXE file) where your formulas, VBA code and workbook file are protected. No complex coding is required.

How do you compile data in Excel?

Click Data>Consolidate (in the Data Tools group). In the Function box, click the summary function that you want Excel to use to consolidate the data. The default function is SUM. Select your data.

How do you assemble a spreadsheet?

Step 1: Open MS Excel. Step 2: Go to Menu and select New >> click on the Blank workbook to create a simple worksheet. OR – Just press Ctrl + N: To create a new spreadsheet. Step 3: By default, Sheet1 will be created as a worksheet in the spreadsheet.


2 Answers

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 :)

like image 32
sehe Avatar answered Oct 03 '22 15:10

sehe


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:

  1. Convert an existing .xlsx file to C# code, importing data from a database.
  2. Allow FlexCel to recalculate the spreadsheet for me.
  3. Read an answer out of a cell (for use elsewhere in my C# code).
  4. 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:

  1. 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.
  2. Yes, FlexCel will recalculate it with XlsFile.Recalc()
  3. Yes, you can read the recalculated values too.
  4. 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)

like image 93
Contango Avatar answered Oct 03 '22 14:10

Contango