- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
以下代码用于获取 Excel 工作簿的自定义文档属性。
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()
.
使用 Application
时从 VSTO 内部来看,代码运行正常,没有任何问题。但是当使用 new Application()
, workbook.CustomDocumentProperties
线抛出 InvalidCastException
:
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
是一个接口(interface),所以使用 new
在界面上对我来说实际上很奇怪。我怎样才能创建一个合适的应用程序来获取 CustomDocumentProperties
?
我正在使用的引用程序集:
最佳答案
I noticed that Microsoft.Office.Interop.Excel.Application is an interface, so using new on interface is actually strange to me.
这确实很奇怪,但这是设计使然。 Excel.Application
接口(interface)用 CoClass
属性修饰,告诉实际类在“实例化”接口(interface)时实例化。更多信息 here .
But when using
new Application()
, theworkbook.CustomDocumentProperties
line throwsInvalidCastException
:
确实又奇怪了。我自己在使用文档属性时遇到了一些问题。似乎返回的实际类与规范不同,所以我转而使用 dynamic
以防止类型转换问题。
所以不是这个:
Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties;
使用:
dynamic properties = workbook.CustomDocumentProperties;
关于c# - 如何使用 Excel Interop 获取 CustomDocumentProperties?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28981686/
以下代码用于获取 Excel 工作簿的自定义文档属性。 var xlApp = Globals.ThisAddIn.Application; // This works in VSTO Excel A
我正在尝试将 DocumentProperty 添加到 CustomDocumentProperties 集合中。代码如下: Sub testcustdocprop() Dim docprops As
我已阅读页面 How do I properly clean up Excel interop objects?但我遇到了一个我无法弄清楚的问题。有一种情况是嵌入了 Excel 实例并且我的插件存在导
我正在尝试在 POC(烟雾和镜子演示)之上组合一个额外的功能。 POC 在 SPS 2007 上,我需要与另一个系统集成。 为了促进其中的一部分,我需要提供一个 JSONP 端点。 我想要这个网址:
我想读出 Word 文档的 BuiltInDocumentProperties/CustomDocumentProperties。以下源总是返回 null :-( using Microsoft.Of
我有一个可以将幻灯片导出到 PowerPoint 的应用程序。我正在使用 Microsoft.Office.Interop.PowerPoint.dll 。这是汇编的第 11 个版本。我今天运行了应用
我是一名优秀的程序员,十分优秀!