Friday, April 15, 2011

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.

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

    Stuck In Baghdad : grrrrr... blocked from work, I'll have to check this from home
  • 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.

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

    Stuck In Baghdad : can you explain more about this? What kind of object is 'worksheet'? How do I get one :P

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.