如何使用 RtdServer 在 C# 中创建实时 Excel 自动化插件?
我的任务是使用 RtdServer 用 C# 编写实时 Excel 自动化插件。我非常依赖在 Stack Overflow 中找到的知识。我决定通过写一份如何记录我所学到的知识来表达我的谢意。 Kenny Kerr 的 Excel RTD 服务器:最小 C# 实现 文章提供了帮助我开始吧。我找到了 Mike Rosenblum 和 Govert 特别有帮助。
I was tasked with writing a real-time Excel automation add-in in C# using RtdServer for work. I relied heavily on the knowledge that I came across in Stack Overflow. I have decide to express my thanks by writing up a how to document that ties together all that I have learned. Kenny Kerr's Excel RTD Servers: Minimal C# Implementation article helped me get started. I found comments by Mike Rosenblum and Govert especially helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
遵循 RTD 服务器的前两个答案对我有用。但是,我在运行 Excel x64 的 x64 计算机上遇到了问题。就我而言,直到我将项目的“目标平台”切换到 x64 之前,Excel 始终显示#N/A。
Following the previous two answers for the RTD server worked for me. However I ran into an issue when on an x64 machine running Excel x64. In my case, until I switched the "target platform" of the project to x64, Excel always showed #N/A.
(作为下面描述的方法的替代方法,您应该考虑使用 Excel-DNA。Excel-DNA 允许您构建免注册 RTD 服务器。COM 注册需要管理权限,这可能会导致安装麻烦。)
要使用 RtdServer 在 C# 中创建实时 Excel 自动化插件,请执行以下操作:
1) 创建一个在 Visual Studio 中创建 C# 类库项目并输入以下内容:
2) 右键单击该项目并添加 >新项目...>>安装人员类别。切换到代码视图并输入以下内容:
3) 右键单击项目属性并选中以下内容:Application >装配信息...>>使程序集 COM 可见并构建 >注册 COM Interop
3.1)右键单击项目添加引用...> .NET 选项卡 > Microsoft.Office.Interop.Excel
4) 构建解决方案 (F6)
5) 运行 Excel。转到 Excel 选项 >加载项>管理 Excel 加载项 >自动化并选择“StackOverflow.RtdServer”
6) 在单元格中输入“=RTD("StackOverflow.RtdServer.ProgId",,200)”。
7)祈祷它能成功!
(As an alternative to the approach described below you should consider using Excel-DNA. Excel-DNA allows you to build a registration-free RTD server. COM registration requires administrative privileges which may lead to installation headaches. That being said, the code below works fine.)
To create a real-time Excel automation add-in in C# using RtdServer:
1) Create a C# class library project in Visual Studio and enter the following:
2) Right click on the project and Add > New Item... > Installer Class. Switch to code view and enter the following:
3) Right click on the project Properties and check off the following: Application > Assembly Information... > Make assembly COM-Visible and Build > Register for COM Interop
3.1) Right click on the project Add Reference... > .NET tab > Microsoft.Office.Interop.Excel
4) Build Solution (F6)
5) Run Excel. Go to Excel Options > Add-Ins > Manage Excel Add-Ins > Automation and select "StackOverflow.RtdServer"
6) Enter "=RTD("StackOverflow.RtdServer.ProgId",,200)" into a cell.
7) Cross your fingers and hope that it works!
从计时器线程调用 UpdateNotify 最终会导致奇怪的错误或与 Excel 断开连接。
UpdateNotify() 方法只能从调用 ServerStart() 的同一线程中调用。 RTDServer 帮助中没有记录它,但它是 COM 的限制。
修复方法很简单。使用 DispatcherSynchronizationContext 捕获调用 ServerStart 的线程并使用它来分派对 UpdateNotify 的调用:
Calling UpdateNotify from the timer thread will eventually cause strange errors or disconnections from Excel.
The UpdateNotify() method must only be called from the same thread that calls ServerStart(). It's not documented in RTDServer help, but it is restriction of COM.
The fix is simple. Use DispatcherSynchronizationContext to capture the thread that calls ServerStart and use that to dispatch calls to UpdateNotify: