Stuck In Baghdad
Stuck In Baghdad

Reputation: 629

how can I access the formula in an excel sheet with c#?

I am parsing thorugh a 2000+ record excel file, and need ot be able to grab the formula itself from the cells and not just the value. For example, one cell 'Contributions' may be a single number like 1500 or it can be a few numbers 1500+500-200 but I dont want the total, I want the actual formula so I can get the different numbers.

I was just parsing with an oledbconnection and that only seems to get me the total. How would I go about getting the formula? Is it possible (I can get it in either XLS or XLSX)?

Thank you.

Upvotes: 1

Views: 4696

Answers (3)

Patrick McDonald
Patrick McDonald

Reputation: 65421

Using OLE Automation, you can access the formula in a cell as follows:

formula = worksheet.Cells(1, 1).Formula

EDIT:

To use OLE Automation in your C# project, see the following Microsoft KB article:

Upvotes: 2

Rune Grimstad
Rune Grimstad

Reputation: 36300

The xslx is an xml file so it should be easy enough to read. Alternatively you can use third-part components like the one from Infragistics that allows you to read xsl-files programmatically and also to access the formulas.

Upvotes: 0

Mitch Wheat
Mitch Wheat

Reputation: 300549

A quick google found the following: http://codingsense.wordpress.com/2009/03/01/get-all-formula-from-excel-cell/

Upvotes: 1

Related Questions