以编程方式访问Excel自定义文档属性

编程入门 行业动态 更新时间:2024-10-25 17:22:52
本文介绍了以编程方式访问Excel自定义文档属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将自定义属性添加到我以编程方式创建的工作簿中。我有一个方法来获取和设置属性,但问题是工作簿为CustomDocumentProperties属性返回null。我无法弄清楚如何初始化此属性,以便我可以从工作簿添加和检索属性。 Microsoft.Office.Core.DocumentProperties是一个接口,所以我不能去做以下

I'm trying to add custom properties to a workbook I have created programatically. I have a method in place for getting and setting properties, but the problem is the workbook is returning null for the CustomDocumentProperties property. I cannot figure out how to initialize this property so that I can add and retrieve properties from the workbook. Microsoft.Office.Core.DocumentProperties is an interface, so I cant go and do the following

if(workbook.CustomDocumentProperties == null) workbook.CustomDocumentProperties = new DocumentProperties;

以下是我必须获取并设置属性的代码:

Here is the code I have to get and set the properties:

private object GetDocumentProperty(string propertyName, MsoDocProperties type) { object returnVal = null; Microsoft.Office.Core.DocumentProperties properties; properties = (Microsoft.Office.Core.DocumentProperties)workBk.CustomDocumentProperties; foreach (Microsoft.Office.Core.DocumentProperty property in properties) { if (property.Name == propertyName && property.Type == type) { returnVal = property.Value; } DisposeComObject(property); } DisposeComObject(properties); return returnVal; } protected void SetDocumentProperty(string propertyName, string propertyValue) { DocumentProperties properties; properties = workBk.CustomDocumentProperties as DocumentProperties; bool propertyExists = false; foreach (DocumentProperty prop in properties) { if (prop.Name == propertyName) { prop.Value = propertyValue; propertyExists = true; } DisposeComObject(prop); if(propertyExists) break; } if (!propertyExists) { properties.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, propertyValue, Type.Missing); } DisposeComObject(propertyExists); }

行属性= workBk.CustomDocumentProperties为DocumentProperties; 始终将属性设置为null。

The line properties = workBk.CustomDocumentProperties as DocumentProperties; always set properties to null.

这是使用Microsoft.Office.Core v12.0.0.0和Microsoft.Office.Interop.Excell v12.0.0。 0(Office 2007)

This is using Microsoft.Office.Core v12.0.0.0 and Microsoft.Office.Interop.Excell v12.0.0.0 (Office 2007)

推荐答案

我看了我自己的代码,可以看到我使用后期绑定访问属性。我不记得为什么,但我会发布一些代码,以防它有帮助。

I looked at my own code and can see that I access the properties using late binding. I can't remember why, but I'll post some code in case it helps.

object properties = workBk.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.Default | BindingFlags.GetProperty, null, workBk, null); object property = properties.GetType().InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, properties, new object[] { propertyIndex }); object propertyValue = property.GetType().InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, propertyWrapper.Object, null);

编辑:啊,现在我记得为什么。 : - )

EDIT: ah, now I remember why. :-)

编辑2 :Jimbojones的答案 - 使用动态关键字 - 是一个更好的解决方案(如果您重视易用性,使用动态的性能开销)。

EDIT 2: Jimbojones' answer - to use the dynamic keyword - is a better solution (if you value ease-of-use over the performance overhead of using dynamic).

更多推荐

以编程方式访问Excel自定义文档属性

本文发布于:2023-11-28 07:36:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641498.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   属性   方式   文档   Excel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!