程序实现RTD Client 从RTD Server取数据

来源:互联网 发布:java调用restful api 编辑:程序博客网 时间:2024/05/18 03:55

前段时间做个项目,是从第三方取行情。但是对方只给了可以跑在Windows上的服务器,其实就是实现了IRTDServer,然后可以用excel里用RTD函数取数据,但是我们需要转发出去,所用必须自己实现程序取出数据,也就是要实现RTD Client。在网上找了很多资料都是讲如何实现RTDserver的,很少有RTDclient的,所以写此文,分享出来。


RTD (real-time- data) 是excel引入的一种获取实时数据的机制,使用函数:=RTD("",,"","")。 要实现取数据,则服务器端要实现 微软定义的一个接口:IRTDServer。

该接口的定义如下:

/// <summary>///  Represents an interface for a real-time data server./// </summary>[Guid("EC0E6191-DB51-11D3-8F3E-00C04F3651B8")][TypeLibType(4160)]public interface IRtdServer2{    /// <summary>    /// Adds new topics from a real-time data server. The ConnectData method is called    ///     when a file is opened that contains real-time data functions or when a user    ///     types in a new formula which contains the RTD function.    /// </summary>    /// <param name="TopicID">    /// Required Integer. A unique value, assigned by Microsoft Excel, which identifies the topic.</param>    /// <param name="Strings">Required Object. A single-dimensional array of strings identifying the topic.</param>    /// <param name="GetNewValues"> Required Boolean. True to determine if new values are to be acquired.</param>    /// <returns></returns>    [DispId(11)]    dynamic ConnectData(int TopicID, ref Array Strings, ref bool GetNewValues);    /// <summary>    /// Notifies a real-time data (RTD) server application that a topic is no longer in use.    /// </summary>    /// <param name="TopicID"> Required Integer. A unique value assigned to the topic assigned by Microsoft Excel.</param>    [DispId(13)]    void DisconnectData(int TopicID);    [DispId(14)]    int Heartbeat();           /// <summary>    /// This method is called by Microsoft Excel to get new data.    /// </summary>    /// <param name="TopicCount">TopicCount:    ///     Required Integer. The RTD server must change the value of the TopicCount    ///     to the number of elements in the array returned.</param>    /// <returns></returns>    [DispId(12)]    Array RefreshData(ref int TopicCount);           /// <summary>    /// The ServerStart method is called immediately after a real-time data server    ///     is instantiated. Negative value or zero indicates failure to start the server;    ///     positive value indicates success.    /// </summary>    /// <param name="CallbackObject">Required Microsoft.Office.Interop.Excel.IRTDUpdateEvent object. The callback object.</param>    /// <returns></returns>    [DispId(10)]    int ServerStart(IRTDUpdateEvent CallbackObject);             /// <summary>    /// Terminates the connection to the real-time data server.    /// </summary>    [DispId(15)]    void ServerTerminate();}

其中,IRTDUpdateEvent的定义如下:

/// <summary>/// Represents real-time data update events./// </summary>[Guid("A43788C1-D91B-11D3-8F39-00C04F3651B8")][TypeLibType(4160)]public interface IRTDUpdateEvent{    [DispId(11)]    int HeartbeatInterval { get; set; }    /// <summary>    ///   Instructs the real-time data server (RTD) to disconnect from the specified    ///   Microsoft.Office.Interop.Excel.IRTDUpdateEvent object.    /// </summary>    [DispId(12)]    void Disconnect();    [DispId(10)]    void UpdateNotify();}

实现了IRTDServer的服务器,其实是个COM组件,excel能用 =RTD()取数据,就是实现加载了该组件。

在这里,我们要实现一个对方签名的server实例:

假设服务签名是 rtd.test,

IRtdServer rtdServer;

Type rtd;            Object _rtdServer = null;            rtd = Type.GetTypeFromProgID(CGlobal.strProgID);            _rtdServer = Activator.CreateInstance(rtd);            rtdServer = _rtdServer as RTDClient.IRtdServer;            this.HeartbeatInterval = 0;            int tes = rtdServer.ServerStart(this);

rtdServer 就是一个server实例。

然后我们定义一个类,继承IRTDUpdateEvent,实现了里面的方法,其中一个方法是 UpdateNotify,则如果有新的数据要更新,server就会调用UpdateNotify, 我们在这里面

接收数据就可以了。



参考:http://www.cnblogs.com/yangecnu/p/Excel-Realtime-Data-Function-Introduce.html






0 0
原创粉丝点击