Search code examples
c#excelworksheet-function

How to parse formula values as text in Excel via C#


I have to parse out some data from a MS Excel file.

The cells that I need to grab data out of have formula text as its value.

For example:

Sheet 2 [A1].Value = "$50"
Sheet 1 [A1].Formula = "='Sheet2'!A1"

When I grab the value for Sheet1 [A1], I get ='Sheet2'!A1, not $50.

Is there a way to get the text value after formula calculation from Sheet1[A1] using C#?


Solution

  • Here is what I got using Microsoft.Office.Interop.Excel as mentioned by Rup

    using Microsoft.Office.Interop.Excel;
    
    string fileName = @"C:\TestSheet.xls";
    Application xlApp = new Application();
    Workbook book = xlApp.Workbooks.Open(fileName);
    Worksheet sheet = xlApp.Worksheets[1];
    Range range = sheet.get_Range("A1");
    Console.WriteLine(range.get_Value(XlRangeValueDataType.xlRangeValueDefault));