【问题标题】:How do I add custom properties to excel document using c#?如何使用 C# 将自定义属性添加到 Excel 文档?
【发布时间】:2020-09-27 14:21:55
【问题描述】:

我正在尝试向 excel 文档添加属性(很少有属性会使文档保密)。我尝试了在互联网上找到的几种方法,但要么出现异常,要么没有任何反应。谁能告诉我哪里出错了?

  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. Try 告诉我:System.ArgumentException:Value 不在预期范围内。
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】:

    您可以使用以下代码:

    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 方法来避免异常。

    【讨论】:

      【解决方案2】:

      对于 c#,它对我有用,

      Microsoft.Office.Core.DocumentProperties prps; prps = (Office.DocumentProperties)this.CustomDocumentProperties; prps.Add("事务类型", false, Microsoft.Office.Core .MsoDocProperties.msoPropertyTypeString,“余额”,缺失);

      这里“交易类型”是关键,“余额”是价值

      【讨论】:

        猜你喜欢
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多