我创建了一个 Excel 自动化插件,作为 C# 类库实现,其中包含 UDF 包装器。(我不使用 VSTO)
我想在我的 UDF 中做的是:
1>获取输入公式的单元格地址;
2>将该单元格的公式设置为我自己的公式之一;
我已经完成了第 1 点,如下所示:
Microsoft.Office.Interop.Excel.Application excelApp = (Microsoft.Office.Interop.Excel.Application)Marshal.GetActiveObject("Excel.Application");
Microsoft.Office.Interop.Excel.Range target = (Microsoft.Office.Interop.Excel.Range)excelApp.get_Caller(System.Type.Missing);
string cellAddress = target.get_Address(Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlReferenceStyle.xlA1, Missing.Value, Missing.Value);
//Perform some operations based on address
//Assign a formula to my cell (This does not work !)
target.Formula ="=SUM(2,2)"
根据 here 的 MSDN 文档,这应该可以工作。 .
我做错了什么?还有其他方法可以设置单元格的公式吗?
谢谢
最佳答案
UDF 只能将值返回到其调用单元格 - 不允许覆盖其自己的公式(或更改任何其他单元格的内容)。您需要创建一个命令来执行此操作。
关于c# - 在 C# 函数中设置 Excel 单元格的公式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8710718/