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?
发布评论
评论(2)
根据此(感谢 PintSizedCat),对于 Excel 2003,会发生以下情况:
在我的测试项目的 Excel 工作簿中,我有两个自定义属性:
_AssemblyName, value = *
_AssemblyLocation, value = {533b2c13-a125-418a-bfff-9546b0762807}
我想这些是将 VSTO 运行时定向到我的程序集的属性。
According to this (thanks PintSizedCat) for Excel 2003 the following happens:
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.
这一切都在注册表中完成,您应该能够在 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).