【发布时间】:2018-02-19 10:14:33
【问题描述】:
在我的控制台应用程序中,我正在尝试向 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;
}
给我以下错误:
下面是异常类对象的转储。
{"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似乎暗示发帖者已经能够成功运行代码了。
你能成功运行代码吗?你见过这个错误并知道它的解决方法吗?
【问题讨论】:
-
如果给定的
Name属性不存在CustomProperty,您应该能够成功调用CustomProperties.Add。即使在 Excel VBA 环境中,Name的检索也从未对WorkSheet.CustomProperties起作用。这是 CustomProperties 的 Worksheet 实现的一个小故障。如果您从 SmartTag 获取 CustomProperties 集合,则可以按名称索引该集合。您链接到的答案显示了如何使用枚举循环按名称进行查找;你试过这种技术吗?
标签: c# excel com ms-office office-interop