无法在控制台应用程序中写入或读取Worksheet.CustomProperties

时间:2017-09-11 08:20:59

标签: c# excel com ms-office office-interop

在我的控制台应用程序中,我正在尝试编写内容并从Excel工作表的CustomProperties读回来。我引用了 Microsoft.Office.Interop.Excel v14程序集。

在调用firstWorksheet.CustomProperties.Add方法的行中,我得到HRESULT 0x800A03EC的异常。

以下是相关的代码:

static void WriteToExcelCustomDocumentProperties(
    string excelFile,
    string outputFolder,
    string propertyName, 
    object propertyValue)
{
    excel::Application excel = null;
    Workbook workbook = null;
    Worksheet firstWorksheet = null;

    try
    {
        excel = new excel::Application();

        workbook = excel.Workbooks.Open(excelFile);

        firstWorksheet = workbook.Worksheets[1] as Worksheet;

        firstWorksheet.CustomProperties.Add(propertyName, propertyValue);

        var outputFilePath = GetOutputFilePath(excelFile, outputFolder);

        workbook.SaveAs(outputFilePath);
    }
    catch(Exception ex)
    {
        Console.WriteLine("\nERROR:");
        Console.WriteLine($"{excelFile}!{firstWorksheet.Name}");
        Console.WriteLine($"{ex.Message}\n");
    }
    finally
    {
        if (workbook != null)
            workbook.Close();

        if (excel != null)
            excel.Quit();
    }
}

以下是我收到的错误:

{"Exception from HRESULT: 0x800A03EC"}
    Data: {System.Collections.ListDictionaryInternal}
    ErrorCode: -2146827284
    HResult: -2146827284
    HelpLink: null
    IPForWatsonBuckets: 0x7177fe49
    InnerException: null
    IsTransient: false
    Message: "Exception from HRESULT: 0x800A03EC"
    RemoteStackTrace: null
    Source: ""
    StackTrace: "   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n   at Microsoft.Office.Interop.Excel.CustomProperties.Add(String Name, Object Value)\r\n   at CustomDocumentProperties.Program.WriteToExcelCustomDocumentProperties(String excelFile, String outputFolder, String propertyName, Object propertyValue) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 61"
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)}
    WatsonBuckets: null
    _HResult: -2146827284
    _className: null
    _data: {System.Collections.ListDictionaryInternal}
    _dynamicMethods: null
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)}
    _exceptionMethodString: null
    _helpURL: null
    _innerException: null
    _ipForWatsonBuckets: 0x7177fe49
    _message: "Exception from HRESULT: 0x800A03EC"
    _remoteStackIndex: 0
    _remoteStackTraceString: null
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
    _source: ""
    _stackTrace: {sbyte[96]}
    _stackTraceString: null
    _watsonBuckets: null
    _xcode: -532462766
    _xptrs: 0x00000000

如果我尝试使用下面列出的代码读取信息,我会得到代码清单之后的异常。

static object ReadFromExcelCustomDocumentProperties(
    string excelFile, 
    string propertyName)
{
    excel::Application excel = null;
    Workbook workbook = null;
    Worksheet firstWorksheet = null;
    object value = null;

    try
    {
        excel = new excel::Application();

        workbook = excel.Workbooks.Open(excelFile);

        firstWorksheet = workbook.Worksheets[1] as Worksheet;

        value = firstWorksheet.CustomProperties[(object)propertyName].Value;
    }
    catch (Exception ex)
    {
        Console.WriteLine($"\nERROR in {nameof(ReadFromExcelCustomDocumentProperties)}:");
        Console.WriteLine($"{excelFile}!{firstWorksheet.Name}");
        Console.WriteLine($"{ex.Message}\n");
    }
    finally
    {
        if (workbook != null)
            workbook.Close();

        if (excel != null)
            excel.Quit();
    }

    return value;
}

给我以下错误:

enter image description here

下面是Exception类对象的转储。

{"Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"}
    Data: {System.Collections.ListDictionaryInternal}
    ErrorCode: -2147352571
    HResult: -2147352571
    HelpLink: null
    IPForWatsonBuckets: 0x7177fe49
    InnerException: null
    IsTransient: false
    Message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    RemoteStackTrace: null
    Source: ""
    StackTrace: "   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)\r\n   at Microsoft.Office.Interop.Excel.CustomProperties.get__Default(Object Index)\r\n   at CustomDocumentProperties.Program.ReadFromExcelCustomDocumentProperties(String excelFile, String propertyName) in C:\\Sathyaish\\DotNet\\CustomDocumentProperties\\CustomDocumentProperties\\Program.cs:line 131"
    TargetSite: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)}
    WatsonBuckets: null
    _HResult: -2147352571
    _className: null
    _data: {System.Collections.ListDictionaryInternal}
    _dynamicMethods: null
    _exceptionMethod: {System.Object ForwardCallToInvokeMember(System.String, System.Reflection.BindingFlags, System.Object, Int32[], System.Runtime.Remoting.Proxies.MessageData ByRef)}
    _exceptionMethodString: null
    _helpURL: null
    _innerException: null
    _ipForWatsonBuckets: 0x7177fe49
    _message: "Type mismatch. (Exception from HRESULT: 0x80020005 (DISP_E_TYPEMISMATCH))"
    _remoteStackIndex: 0
    _remoteStackTraceString: null
    _safeSerializationManager: {System.Runtime.Serialization.SafeSerializationManager}
    _source: ""
    _stackTrace: {sbyte[96]}
    _stackTraceString: null
    _watsonBuckets: null
    _xcode: -532462766
    _xptrs: 0x00000000

this answer看来,上述观察到的行为可能归因于互操作程序集中的错误。

但是,this answer似乎表明海报能够成功运行代码。

您是否能够成功运行代码?你有没有看到这个错误并知道它的修复?

1 个答案:

答案 0 :(得分:0)

我可能不知道你对自定义属性的意思,但如果你指的是Excel中的File-> Info部分的属性:

enter image description here

与来自Sharepoint的那些一样,我使用ContentTypeProperties对象的Workbook集合访问它们。

以下是我将如何在上图中访问它们的示例:

// Excel.Workbook wb;

string dmdRegion = wb.ContentTypeProperties["Demand Region"].Value.ToString();
wb.ContentTypeProperties["Demand Region"].Value = "EMEA";

你的例子展示了来自Worksheet对象的东西,所以我可能完全错过了这条船。