如何使用C#Excel Interop读取Excel自定义文档属性 [英] How can I read excel custom document property using c# excel interop

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

问题描述

我正在尝试检查是否已为excel文件设置了自定义文档属性.如果已设置,则读取该值.

I'm trying to check if a custom document property has been set for an excel file or not. And if set then read the value.

这是我正在使用的代码,但到目前为止还没有运气.它不会进入foreach循环并显示出来.

Here is the code I'm using but so far no luck. It doesn't get into the foreach loop and comes out.

var propval = ReadDocumentProperty("TestProp");

private string ReadDocumentProperty(string propertyName)
{
    Office.DocumentProperties properties;
    Excel.Workbook Wb = Globals.ThisAddIn.Application.ActiveWorkbook;
    properties = (Office.DocumentProperties)Wb.CustomDocumentProperties;

    foreach (Office.DocumentProperty prop in properties)
    {
        if (prop.Name == propertyName)
        {
            return prop.Value.ToString();
        }
    }
    return null;
}

更新1:

我找到了用于设置自定义属性的代码.

I found this code for setting the custom property.

Excel.Workbook workBk = Globals.ThisAddIn.Application.ActiveWorkbook;

            object oDocCustomProps = workBk.CustomDocumentProperties;
            Type typeDocCustomProps = oDocCustomProps.GetType();

            object[] oArgs = {propertyName,false,
         Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
         propertyValue};

            typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                       BindingFlags.InvokeMethod, null,
                                       oDocCustomProps, oArgs);

这可以很好地设置自定义属性.我不知道如何修改它以读取属性值.

This works fine to set the custom property. I have no clue how to modify it to read the property value.

推荐答案

原理相同.当需要使用Office"interop"时,一些有关如何使用PInvoke的研究会有所帮助.为了使用它,必须充分理解您需要解决的Office对象模型的一部分:对象,属性或方法以及确切需要哪些参数,因为没有IntelliSense可以帮助.在VBA界面中进行首次测试可以使此操作变得更容易.

The principle is the same. Some research into how to use PInvoke would help when it's required for working with the Office "interop". In order to use it, it's necessary to fully understand the part of the Office object model you need to address: the object, the property or method and exactly what arguments are required as there is no IntelliSense that can help. First testing in the VBA interface can make this easier.

我在测试项目中拥有的以下代码段演示了如何处理单个Document属性并进行读取,然后写入其值.请注意,示例代码可与BuiltInDocumentProperties一起使用.如果需要的话,可以将其更改为CustomDocumentProperties.

The following code snippet which I have in a test project demonstrates how to address a single Document Property and read, then write its value. Note that the sample code works with BuiltInDocumentProperties. This can be changed to CustomDocumentProperties if that's what is required.

    private void btnUpdateCustomDocProp_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
        Excel.Application xlApp = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.application");
        Excel.Workbook wb = xlApp.ActiveWorkbook;
        object docProps = wb.BuiltinDocumentProperties;

        object prop = ExistsDocProp("Author", docProps);
        if (prop!=null)
        {
            object oProp = prop;
            Type oPropType = oProp.GetType();
            //read current value
            string propValue = oPropType.InvokeMember("Value",
                BindingFlags.GetProperty | BindingFlags.Default,
                null, oProp, new object[] { }).ToString();

            object oPropValue = "new test author";
            //write new value
            oPropType.InvokeMember("Value",
                BindingFlags.SetProperty | BindingFlags.Default,
                null, oProp, new object[] {oPropValue});

            MessageBox.Show(propValue + ", " + oPropValue.ToString());         
        }
    }

    private object ExistsDocProp(string propName, object props)
    {
        Office.DocumentProperty customDocProp = null;
        Type docPropsType = props.GetType();
        object nrProps;
        object itemProp = null;
        object oPropName;

        nrProps = docPropsType.InvokeMember("Count",
            BindingFlags.GetProperty | BindingFlags.Default,
            null, props, new object[] { });
        int iProps = (int)nrProps;

        for (int counter = 1; counter <= ((int)nrProps); counter++)
        {
            itemProp = docPropsType.InvokeMember("Item",
                BindingFlags.GetProperty | BindingFlags.Default,
                null, props, new object[] { counter });

            oPropName = docPropsType.InvokeMember("Name",
                BindingFlags.GetProperty | BindingFlags.Default,
                null, itemProp, new object[] { });

            if (propName == oPropName.ToString())
            {
                break;
            }
        }
        return itemProp; 
    }

这篇关于如何使用C#Excel Interop读取Excel自定义文档属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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