c# - 如何更新内置 Excel 文档属性(文档元数据)

标签 c# asp.net excel

using Excel = Microsoft.Office.Interop.Excel;

我正在使用下面的方法添加自定义属性,但如果属性已存在,它会显示异常。我想添加自定义属性,如果它存在应该更新。

这是我用来添加属性的代码

  Excel.Workbook workBk;
  Application _excelApp;

 public void SetDocumentProperty(string propertyName, string propertyValue)
    {
        try
        {
            _excelApp = new Application();
            workBk = _excelApp.Workbooks.Open(@"C:\12345.xlsx",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

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

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

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

        finally
        {
            workBk.Close(false, @"C:\12345.xlsx", null);
            Marshal.ReleaseComObject(workBk);
        }

    }

最佳答案

试试这个

 public void SetDocumentProperty(string propertyName, string propertyValue)
    {
        try
        {
            _excelApp = new Application();
            workBk = _excelApp.Workbooks.Open(@"C:\12345.xlsx",
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing);

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

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

            typeDocCustomProps.InvokeMember("Add", BindingFlags.Default |
                                       BindingFlags.InvokeMethod, null,
                                       oDocCustomProps, oArgs);
        }
        catch
        {
            object customProperties = workBk.CustomDocumentProperties;
            Type customPropertiesType = customProperties.GetType();

            // Retrieve the specific custom property item
            object customPropertyItem = customPropertiesType.InvokeMember("Item",
                BindingFlags.Default | BindingFlags.GetProperty, null, customProperties,
                new object[] { propertyName });
            Type propertyNameType = customPropertyItem.GetType();

            propertyNameType.InvokeMember("Value", BindingFlags.Default | BindingFlags.SetProperty, null,
            customPropertyItem, new object[] { propertyValue });
        }

        finally
        {
            workBk.Save();
            workBk.Close(false, @"C:\12345.xlsx", null);
            Marshal.ReleaseComObject(workBk);
        }

    }

关于c# - 如何更新内置 Excel 文档属性(文档元数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31420877/

相关文章:

arrays - Excel 2013 VBA Range.RemoveDuplicates 指定数组的问题

C# DynamicPDF 合并导致 "Index out of bounds"错误

C# Cosmos DB 补丁 - 将元素插入子集合

c# - 如何完全获取c#中方法的路径?

asp.net - 在 asp :Panel? 中隐藏 DIV

.net - 在 .Net 中解析自定义 JSON 对象?

javascript - Office.Js Row单击事件

c# - Context.User 在 Global.asax.Application_AuthenticateRequest 中分配后失去角色

asp.net - 文件将文件上传到 Azure Blob 存储

excel - Excel 是否计算提供给 IF 函数的两个结果参数?