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

在我的控制台应用程序中,我试图写一些东西,并从Excel工作表的CustomProperties中读回来。 我有一个参考Microsoft.Office.Interop.Excel v14程序集。

在调用firstWorksheet.CustomProperties.Add方法的行firstWorksheet.CustomProperties.Add ,我得到一个HRESULT 0x800A03ECexception。

以下是相关的代码位:

 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 

如果我尝试阅读使用下面列出的代码的信息,我得到的代码列表后的exception。

 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; } 

给我以下错误:

在这里输入图像说明

下面是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 

从这个答案看来,上面观察到的行为可能是由于互操作程序集中的一个错误。

但是, 这个答案似乎表明海报已经能够成功运行代码。

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

有可能我不知道自定义属性是什么意思,但是如果您是从Excel中的文件 – >信息部分引用属性:

在这里输入图像说明

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

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

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

你的例子显示了Worksheet对象中的一些东西,所以我可能完全错过了这条船。