Excel VSTO 如何工作?

发布于 2024-07-14 00:23:30 字数 358 浏览 11 评论 0 原文

Excel VSTO 如何工作? 如果我在 Visual Studio 2005 中创建 Excel 工作簿解决方案,那么我就可以愉快地编写代码并完全访问 Excel 对象模型,甚至将 Excel 工作表视为设计图面。 当我构建解决方案时,我得到一个 .XLS 文件和一个 .DLL (包含我的 C# 代码)。

现在,我只需双击 .XLS 即可启动 Excel 工作表,我的工作表可与我的所有 C# 代码以及我放在工作表上的任何控件等一起运行。

工作表如何引用.DLL? Excel 工作簿/工作表的哪一部分告诉它需要启动 CLR 并托管我的程序集?

How does Excel VSTO Work? If I create an Excel Workbook solution in Visual Studio 2005 I can then happily code away with full access to the Excel object model and even treat the Excel sheet as a design surface. When I build the solution I get a .XLS file and a .DLL (containing my C# code).

I can now start up the Excel sheet just by double clicking on the .XLS and there is my sheet functioning with all my C# code and any controls I dropped on the sheet etc.

How is the sheet referencing the .DLL? What part of the excel workbook/sheet tells it that it needs to fire up the CLR and host my assembly?

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

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

发布评论

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

评论(2

云巢 2024-07-21 00:23:30

根据(感谢 PintSizedCat),对于 Excel 2003,会发生以下情况:

Microsoft Office 应用程序
检查自定义文档属性
查看是否有托管代码
与相关的扩展
文档。 有关更多信息,请参阅
自定义文档属性概述。

如果有托管代码扩展,
应用程序加载AddinLoader.dll。
这是一个非托管 DLL,即
Visual Studio 的加载器组件
2005 Office 工具第二版
运行。 有关更多信息,请参阅
用于 Office 运行时的 Visual Studio 工具
概述。

AddinLoader.dll 加载 .NET
框架并启动托管
Visual Studio 工具的一部分
Office 运行时。

Office 的 Visual Studio 工具
运行时创建一个应用程序域,
设置应用程序域的策略
不要信任“我的电脑区域”,并且
检查代码访问安全策略
商店查找政策
定制组装。

.NET Framework 验证
大会提出的证据
反对政策。 如果失败,则会出现
出现错误。 如果通过,则
过程仍在继续。

如果自定义使用部署
清单,Visual Studio 工具
Office 运行时使用它来检查
装配更新。 如果有任何更新
有必要,现在就进行。

Office 的 Visual Studio 工具
运行时将程序集加载到
应用程序域。

Office 的 Visual Studio 工具
运行时调用启动事件
您的自定义处理程序
集会。 有关更多信息,请参阅
用于 Office 项目的 Visual Studio 工具
活动。

在我的测试项目的 Excel 工作簿中,我有两个自定义属性:

_AssemblyName, value = *
_AssemblyLocation, value = {533b2c13-a125-418a-bfff-9546b0762807}

我想这些是将 VSTO 运行时定向到我的程序集的属性。

According to this (thanks PintSizedCat) for Excel 2003 the following happens:

The Microsoft Office application
checks the custom document properties
to see whether there are managed code
extensions associated with the
document. For more information, see
Custom Document Properties Overview.

If there are managed code extensions,
the application loads AddinLoader.dll.
This is an unmanaged DLL that is the
loader component for the Visual Studio
2005 Tools for Office Second Edition
runtime. For more information, see
Visual Studio Tools for Office Runtime
Overview.

AddinLoader.dll loads the .NET
Framework and starts the managed
portion of the Visual Studio Tools for
Office runtime.

The Visual Studio Tools for Office
runtime creates an application domain,
sets policy for the application domain
not to trust the My Computer Zone, and
checks the code access security policy
store to find a policy for the
customization assembly.

The .NET Framework validates the
evidence presented by the assembly
against the policy. If it fails, an
error is raised. If it passes, the
process continues.

If the customization uses a deployment
manifest, the Visual Studio Tools for
Office runtime uses it to check for
assembly updates. If any updates are
necessary, they are performed now.

The Visual Studio Tools for Office
runtime loads the assembly into the
application domain.

The Visual Studio Tools for Office
runtime calls the Startup event
handler in your customization
assembly. For more information, see
Visual Studio Tools for Office Project
Events.

In my test project's Excel workbook I have two custom properties:

_AssemblyName, value = *
_AssemblyLocation, value = {533b2c13-a125-418a-bfff-9546b0762807}

I suppose these are the properties which direct the VSTO runtime to my assembly.

毅然前行 2024-07-21 00:23:30

这一切都在注册表中完成,您应该能够在 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Excel 或等效应用程序中找到该密钥。 我对 COM 插件有更多的经验,这些插件也在注册表中的其他地方注册。 该键下应该有一个 LoadBehaviour 项,用于确定应用程序如何加载(2 是手动加载,3 是启动时自动加载)。

您有 VSTO 的安装项目吗? 在里面你可以看到设置的注册表项,但安装程序也会/应该在 GAC 中注册 VSTO(尽管不要相信我的话,因为正如我所说,我对 VSTO 有点犹豫)。

希望这对您有所帮助,我会尽力为您找到更多信息。

编辑
您应该尝试阅读以下内容 http://msdn.microsoft.com/en-us /library/bb386298.aspx 这将为您解释该插件是什么。 它实际上只是一个从注册表加载的 COM 主机的包装器,VSTO 使用一些互操作性代码与之对话。

同样有用的是 http://msdn.microsoft.com/en-us/library/ 23cw517s.aspx(Visual Studio Tools for Office 入门,不要敲它,因为它说入门,里面有很多有用的信息)和 http://msdn.microsoft.com/en-us/library/hy7c6z9k.aspx (从第一个链接并且是 VSTO 插件的概述)。

This is all done in the Registry, you should be able to find the key in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Office\Excel or your equivelant application. I've more experience with COM Addins which are registered somewhere else in the Registry as well. This key should have a LoadBehaviour item under it which is used to determine how the application is loaded (2 is load manually, 3 is load automatically on startup).

Do you have a Setup Project for your VSTO? Inside there you can see the Registry key that is set, but the Setup program will/should also Register the VSTO in GAC (though don't take my word for it as I'm a bit shakey with VSTO as I said).

Hope this helps, I shall try and find some more information for you.

Edit
You should try reading the following http://msdn.microsoft.com/en-us/library/bb386298.aspx which will give you an explanation of what the addin is. It's really just a wrapper around a COM host which is loaded from the Registry and the VSTO talks to that using some Interoparability code.

Also useful are http://msdn.microsoft.com/en-us/library/23cw517s.aspx (Getting Started with Visual Studio Tools for Office, don't knock it because it says Getting Started in, there's a lot of useful info in there) and http://msdn.microsoft.com/en-us/library/hy7c6z9k.aspx (Which is linked from the first and is an overview of VSTO Addins).

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