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

时间:2015-07-15 03:15:54

标签: 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);
        }

    }

1 个答案:

答案 0 :(得分:0)

试试这个

 class MyFirstComponentMixin(ComponentMixin):
      xmlid = 'component1'
      var = 'one'

 class MySecondComponentMixin(ComponentMixin):
      xmlid = 'component2'
      var = 'two'

 class MyTest(BaseTest, MyFirstComponentMixin, MySecondComponentMixin, unittest.TestCase):
      xmlfile = '...'
相关问题