如何在C#中创建包装RTD函数的Excel自动化加载项? [英] How do I create an Excel automation add-in in C# that wraps an RTD function?

查看:476
本文介绍了如何在C#中创建包装RTD函数的Excel自动化加载项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的基于RtdServer的自动化加载项:
如何创建真实的RtdServer在C#中使用实时Excel自动化加载项?.

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

创建VBA包装器很简单:

Creating a VBA wrapper is trivial:

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

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

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

当我在Excel的单元格中输入"= Countdown(150)"时,它将显示150的初始值,该初始值由ConnectData返回但从未更新.我应该注册一些回调吗?我是否正确实例化了Application对象?我想念什么?

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?

谢谢

坦率

推荐答案

实际上,您没有掌握正确的Application对象.一种解决方案是实施 IDTExtensibility2 界面.此接口具有一个OnConnection方法,Excel在加载外接程序时将调用它.在此方法中,将为您传递Application对象,您可以将其保存在局部变量中以备后用.

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.

这篇关于如何在C#中创建包装RTD函数的Excel自动化加载项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆