我正在与Microsoft.Office.Interop.Excel
我想将运行公式的结果提取到我的代码中。

我正在做:运行公式,将结果保存在单元格中,将其拉入变量,清除单元格,将结果返回为:

 ...
 rangeObject = sheetObject.Range["myRange"];
 rangeObject.Formula ="myFormula";
 string value = rangeObject.Value.ToString();
 sheetObject.Range["myRange"].Clear();
 return value;
 ...


可以在没有rangeObject.Formula ="myFormula";的情况下运行rangeObject吗?

有直接的方法吗?

PS:可以使用剪贴板吗?

最佳答案

根据the documentationEvaluate对象的Excel.Application方法(应该在Interop中可用)可以用于直接计算Microsoft Excel中的公式。

例:

// Cells A1 and B1 of the current worksheet contain 3 and 5, respectively.

var myResult = myExcelApp.Evaluate("A1 + B1"); // Yields 8.

08-25 22:32
查看更多