簡體   English   中英

如何使用 Excel Interop 獲取 CustomDocumentProperties?

[英]How to get CustomDocumentProperties using Excel Interop?

以下代碼用於獲取 Excel 工作簿的自定義文檔屬性。

var xlApp = Globals.ThisAddIn.Application; // This works in VSTO Excel Add-in
var xlApp = new global::Microsoft.Office.Interop.Excel.Application(); // This doesn't work anywhere
xlApp.Visible = true;
global::Microsoft.Office.Interop.Excel.Workbook workbook = xlApp.Workbooks.Open(file, false, true, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, false, Type.Missing, Type.Missing);

global::Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties; // Exception occurs here
global::Microsoft.Office.Core.DocumentProperty property = properties["propertyname"];

前兩行是對 Excel Application引用。 一個從 VSTO 插件內部獲取引用,另一個是常規的new Application()

從 VSTO 內部使用Application ,代碼運行良好,沒有任何問題。 但是當使用new Application()workbook.CustomDocumentProperties行會拋出InvalidCastException

無法將類型為“System.__ComObject”的 COM 對象轉換為接口類型“Microsoft.Office.Core.DocumentProperties”。 此操作失敗,因為 IID 為“{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}”的接口的 COM 組件上的 QueryInterface 調用因以下錯誤而失敗:不支持此類接口(來自 HRESULT 的異常:0x80004002 (E_NOINTERFACE)) .

我試圖讓它在沒有 VSTO 的 C# winforms 項目上工作。 很多示例和教程使用new Application()進行 Excel 互操作,但我注意到Microsoft.Office.Interop.Excel.Application是一個接口,因此在接口上使用new對我來說實際上很陌生。 如何創建可以獲取CustomDocumentProperties的正確應用程序?

我正在使用的參考程序集:

  • Microsoft.Office.Interop.Excel v2.0.50727 版本 14.0.0.0
  • Microsoft.CSharp v4.0.30319 版本 4.0.0.0

我注意到 Microsoft.Office.Interop.Excel.Application 是一個接口,所以在接口上使用 new 對我來說實際上很陌生。

這確實很奇怪,但有意為之。 Excel.Application接口用CoClass屬性裝飾,告訴實際類在“實例化”接口時實例化。 更多關於它在這里

但是當使用new Application()workbook.CustomDocumentProperties行會拋出InvalidCastException

果然又奇怪了。 我自己在使用文檔屬性時遇到了一些問題。 返回的實際類似乎與規范不同,因此我改為使用dynamic以防止類型轉換問題。

所以而不是這個:

Microsoft.Office.Core.DocumentProperties properties = workbook.CustomDocumentProperties;

用:

dynamic properties = workbook.CustomDocumentProperties;

如何創建可以獲取 CustomDocumentProperties 的正確應用程序?

如果您開發加載項,則無需創建新的 Excel 應用程序實例。 您應該使用 VSTO 運行時提供的 Application 屬性:

var xlApp = Globals.ThisAddIn.Application; // This works in VSTO Excel Add-in

但是,如果您開發了一個自動化 Excel 的獨立應用程序,在這種情況下,您需要使用 new 運算符創建一個新的 Application 實例:

var xlApp = new global::Microsoft.Office.Interop.Excel.Application(); 

使用后期綁定技術 (Type.InvokeMember) 獲取或設置文檔屬性,如如何使用自動化獲取和設置 Office 文檔屬性與 Visual C# .NET文章所建議的那樣。

我遇到了同樣的問題。 今天已經解決了。 有一種不同的方法來得出結果。 問題及其答案在

如何使用 c# excel interop 讀取 excel 自定義文檔屬性

這是我的實現。

public string CheckDocProp(string propName, object props)
        {
            Excel.Workbook workBk = Globals.ThisAddIn.Application.ActiveWorkbook;

            object customProperties = workBk.CustomDocumentProperties;
            Type docPropsType = customProperties.GetType();
            object nrProps;
            object itemProp = null;
            object oPropName;
            object oPropVal = null;

            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())
                {
                    oPropVal = docPropsType.InvokeMember("Value",
                        BindingFlags.GetProperty | BindingFlags.Default,
                        null, itemProp, new object[] { });
                    return oPropVal.ToString();
                    break;
                }
                else
                {
                    return "Not Found.";
                }
            }
            return "Not Found.";
        }

用法:

object docProps = wb.CustomDocumentProperties;
            string prop1 = ExistsDocProp("<CustomProperty>", docProps);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM