如何使用 C# 创建包装 RTD 函数的 Excel 自动化插件?

发布于 2024-10-25 15:52:03 字数 1440 浏览 3 评论 0原文

我有一个基于 RtdServer 的可用自动化插件:
如何创建真实的-使用 RtdServer 在 C# 中使用 Excel 自动化插件?

创建 VBA 包装器很简单:

Function RtdWrapper(start)
    RtdWrapper = Excel.Application.WorksheetFunction.RTD("StackOverflow.RtdServer.ProgId", "", start)
End Function

这很有效。我尝试创建一个 C# 包装器,如下所示:

[ClassInterface(ClassInterfaceType.AutoDual)]
public class RtdWrappers
{
    private readonly Microsoft.Office.Interop.Excel.Application _application = new Application();

    public object Countdown(object startingCount)
    {
        var start = Convert.ToInt32(startingCount.ToString());
        return _application.WorksheetFunction.RTD("StackOverflow.RtdServer.ProgId", string.Empty, start);
    }

    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
    }

    [ComUnregisterFunctionAttribute]
    public static void UnregisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey("CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
    }
}

当我在 Excel 的单元格中输入“=Countdown(150)”时,它显示初始值 150,该值由 ConnectData 返回但从未更新。我应该注册一些回调吗?我是否正确实例化了应用程序对象?我缺少什么?

谢谢,

弗兰克

I have a working RtdServer-based automation add-in:
How do I create a real-time Excel automation add-in in C# using RtdServer?.

Creating a VBA wrapper is trivial:

Function RtdWrapper(start)
    RtdWrapper = Excel.Application.WorksheetFunction.RTD("StackOverflow.RtdServer.ProgId", "", start)
End Function

This works. I have attempted to create a C# wrapper as follows:

[ClassInterface(ClassInterfaceType.AutoDual)]
public class RtdWrappers
{
    private readonly Microsoft.Office.Interop.Excel.Application _application = new Application();

    public object Countdown(object startingCount)
    {
        var start = Convert.ToInt32(startingCount.ToString());
        return _application.WorksheetFunction.RTD("StackOverflow.RtdServer.ProgId", string.Empty, start);
    }

    [ComRegisterFunctionAttribute]
    public static void RegisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.CreateSubKey("CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
    }

    [ComUnregisterFunctionAttribute]
    public static void UnregisterFunction(Type t)
    {
        Microsoft.Win32.Registry.ClassesRoot.DeleteSubKey("CLSID\\{" + t.GUID.ToString().ToUpper() + "}\\Programmable");
    }
}

When I enter "=Countdown(150)" into a cell in Excel it shows the initial value of 150 which is returned by ConnectData but never updates. Is there some callback that I should register? Am I instantiating the Application object correctly? What am I missing?

Thanks,

Frank

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无所的.畏惧 2024-11-01 15:52:03

Indeed, you are not getting hold of the right Application object. One solution is to implement the IDTExtensibility2 interface in your add-in. This interface has an OnConnection method that Excel will call when loading your add-in. In this method you are passed the Application object which you can keep in a local variable for later use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文