如何使用Excel Interop获取CustomDocumentProperties? [英] How to get CustomDocumentProperties using Excel Interop?

查看:137
本文介绍了如何使用Excel Interop获取CustomDocumentProperties?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码用于获取Excel工作簿的自定义文档属性.

The below code is used to get the custom document properties for Excel workbook.

var xlApp = Globals.ThisAddIn.Application; // This works in VSTO Excel Add-in
var xlApp = new global::Microsoft.Office.Interop.Excel.Application(); // This doesn't work anywhere
xlApp.Visible = true;
global::Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Open(file, false, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, Type.Missing);

global::Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties; // Exception occurs here
global::Microsoft.Office.Core.DocumentProperty property = properties["propertyname"];

前两行是对Excel Application的引用.一个是从VSTO插件内部获取引用的,另一个是常规的new Application().

The first 2 lines are references to the Excel Application. One obtain the reference from VSTO add-in internals, the other is a regular new Application().

在VSTO内部使用Application时,代码运行正常,没有任何问题.但是,当使用new Application()时,workbook.CustomDocumentProperties行会抛出InvalidCastException:

When using the Application from VSTO internals, the code run fines without any problems. But when using new Application(), the workbook.CustomDocumentProperties line throws InvalidCastException:

无法将类型为'System .__ ComObject'的COM对象转换为接口 键入"Microsoft.Office.Core.DocumentProperties".该操作失败 因为接口的COM组件上的QueryInterface调用 IID为'{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}'的原因是 出现以下错误:不支持这样的接口(HRESULT的异常: 0x80004002(E_NOINTERFACE)).

Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

我正在尝试使其在没有VSTO的C#winforms项目上工作.许多示例和教程将new Application()用于Excel互操作,但是我注意到Microsoft.Office.Interop.Excel.Application是一个接口,因此在接口上使用new实际上对我来说很奇怪.如何创建可以获取CustomDocumentProperties的适当应用程序?

I am trying to make it to work on a C# winforms project without VSTO. A lot of examples and tutorials use new Application() for Excel interop, but I noticed that Microsoft.Office.Interop.Excel.Application is an interface, so using new on interface is actually strange to me. How can I create a proper Application that can get the CustomDocumentProperties?

我正在使用的参考程序集:

Reference Assemblies I am using:

  • Microsoft.Office.Interop.Excel v2.0.50727版本14.0.0.0
  • Microsoft.CSharp v4.0.30319版本4.0.0.0

推荐答案

我注意到Microsoft.Office.Interop.Excel.Application是一个接口,因此实际上使用new on接口对我来说很奇怪.

I noticed that Microsoft.Office.Interop.Excel.Application is an interface, so using new on interface is actually strange to me.

这确实很奇怪,但是是设计使然. Excel.Application接口装饰有CoClass属性,该属性告诉实际的类在实例化"该接口时实例化.有关更多信息,请此处.

That is strange indeed, but by design. The Excel.Application interface is decorated with the CoClass attribute telling the actual class to instantiate on 'instantiating' the interface. More about it here.

但是使用new Application()时,workbook.CustomDocumentProperties行会抛出InvalidCastException:

确实再次奇怪.我自己在使用文档属性时遇到了一些问题.似乎返回的实际类与规范不同,因此我移至使用dynamic以避免类型转换问题.

Strange indeed again. I have experienced some issues myself using document properties. It seems that the actual class returned differs from the spec, so I moved to use dynamic in order to prevent type casting issues.

所以代替这个:

Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties;

使用:

dynamic properties = workbook.CustomDocumentProperties;

这篇关于如何使用Excel Interop获取CustomDocumentProperties?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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