如何使用c#将自定义属性添加到excel文档?

问题描述 投票:0回答:1

我正在尝试将属性添加到excel文档中(少数属性会使文档变得机密)。我尝试了几种在Internet上找到的方法,但是要么出现异常,要么什么也没有发生。有人可以告诉我我在哪里犯错吗?

  1. 方式:在这里,我得到System.Reflection.TargetParameterCountException。

     class Program
    {
        static Application excelApp;
        static Workbook excelWorkBook;
        static Worksheet excelWorkSheet;

        string name1 = "myprop1";
        string val1 = "myvalue1";

        string name2 = "myprop2";
        string val2 = "myvalue2";

        string name3 = "longid";
        int val3 = 3;

        string name4 = "id";
        int val4 = 2;


        excelWorkBook.CustomDocumentProperties(name1,val1);
        excelWorkBook.CustomDocumentProperties(name2, val2);
        excelWorkBook.CustomDocumentProperties(name3, val3);
        excelWorkBook.CustomDocumentProperties(name4, val4);
    }

  1. 方式:这里什么也没发生。属性未更改,也不例外。
 excelWorkSheet.CustomProperties.Add("myprop1", "myvalue1");
 excelWorkSheet.CustomProperties.Add("myprop2", "myvalue2");
 excelWorkSheet.CustomProperties.Add("myprop3", 3);
 excelWorkSheet.CustomProperties.Add("myprop4", 2);
  1. 尝试给我:System.ArgumentException:值不在预期范围内。
excelWorkBook.CustomDocumentProperties["myprop1"].Text = "myvalue1";
excelWorkBook.CustomDocumentProperties["myprop2"].Text = "myvalue2";
excelWorkBook.CustomDocumentProperties["myprop3"].Value = 3;
excelWorkBook.CustomDocumentProperties["myprop4"].Value = 2;
c# excel automation com office-interop
1个回答
0
投票

您可以使用以下代码:

Word.Document doc = null;
    Microsoft.Office.Core.DocumentProperties docprops = null;
    Microsoft.Office.Core.DocumentProperty projectNameProperty = null;

    try
    {
        doc = WordApp.ActiveDocument;
        docprops = doc.CustomDocumentProperties as 
            Microsoft.Office.Core.DocumentProperties;
        projectNameProperty = docprops.Add(
            "Customer Name",
            false,
            Microsoft.Office.Core.MsoDocProperties.msoPropertyTypeString,
            "Add-in Express");
        doc.Save();
    }
    finally
    {
        if (projectNameProperty != null)
            Marshal.ReleaseComObject(projectNameProperty);
        if (docprops != null) Marshal.ReleaseComObject(docprops);
        if (doc != null) Marshal.ReleaseComObject(doc);
    }

要修改自定义属性,可以使用以下代码:

 Word.Document doc = null;
    Microsoft.Office.Core.DocumentProperties docprops = null;
    Microsoft.Office.Core.DocumentProperty projectNameProperty = null;

    try
    {
        doc = WordApp.ActiveDocument;
        docprops = doc.CustomDocumentProperties as 
            Microsoft.Office.Core.DocumentProperties;
        projectNameProperty = docprops["Customer Name"];
        projectNameProperty.Value = "Acme Inc.";
        doc.Save();
    }
    finally
    {
        if (projectNameProperty != null) 
            Marshal.ReleaseComObject(projectNameProperty);
        if (docprops != null) Marshal.ReleaseComObject(docprops);
        if (doc != null) Marshal.ReleaseComObject(doc);
    }

Working with Word document properties, bookmarks, content controls and quick parts文章中阅读有关文档属性的更多信息。

如果您在访问文档属性时仍然遇到问题,我建议使用后期绑定技术,方法是使用Type.InvokeMember方法来避免出现异常。

© www.soinside.com 2019 - 2024. All rights reserved.