开发者

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#?开发者_开发百科


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


I'm not sure which member of Range handles that set but I'd guess it equates to Value2 and not a general input in the same way as the spreadsheet. Try:

Sheet1[A1].Formula = "='Sheet2'!A1";
Console.Out.WriteLine(Sheet1[A1].Value2);

to explicitly set that as a forumula. You might also need to trigger a recalculation but I'd expect not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜