【发布时间】:2010-11-11 09:16:39
【问题描述】:
我正在尝试将自定义属性添加到我以编程方式创建的工作簿中。我有一个获取和设置属性的方法,但问题是工作簿为 CustomDocumentProperties 属性返回 null。我无法弄清楚如何初始化此属性,以便我可以从工作簿中添加和检索属性。 Microsoft.Office.Core.DocumentProperties 是一个接口,所以我不能去执行以下操作
if(workbook.CustomDocumentProperties == null)
workbook.CustomDocumentProperties = new DocumentProperties;
这是我必须获取和设置属性的代码:
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。
这是使用 Microsoft.Office.Core v12.0.0.0 和 Microsoft.Office.Interop.Excell v12.0.0.0 (Office 2007)
【问题讨论】:
标签: c# excel automation