C# 无法在控制台应用程序中写入或读取工作表.CustomProperties
在我的控制台应用程序中,我试图在Excel工作表的C# 无法在控制台应用程序中写入或读取工作表.CustomProperties,c#,excel,com,ms-office,office-interop,C#,Excel,Com,Ms Office,Office Interop,在我的控制台应用程序中,我试图在Excel工作表的CustomProperties中写入一些内容并将其读回。我引用了Microsoft.Office.Interop.Excelv14程序集 在调用firstWorksheet.CustomProperties.Add方法的那一行,我得到了一个HRESULT 0x800A03EC异常 以下是相关的代码位: static void WriteToExcelCustomDocumentProperties( string excelFile,
CustomProperties
中写入一些内容并将其读回。我引用了Microsoft.Office.Interop.Excelv14程序集
在调用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;
}
给我以下错误:
下面是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的内容一样,我使用
工作簿
对象的ContentTypeProperties
集合来访问它们
下面是我如何在上图中访问它们的示例:
// Excel.Workbook wb;
string dmdRegion = wb.ContentTypeProperties["Demand Region"].Value.ToString();
wb.ContentTypeProperties["Demand Region"].Value = "EMEA";
您的示例显示了
工作表
对象中的某些内容,因此,我可能完全错过了机会。我可能不知道您所说的自定义属性是什么意思,但如果您是指Excel中“文件->信息”部分中的属性:
与来自Sharepoint的内容一样,我使用工作簿
对象的ContentTypeProperties
集合来访问它们
下面是我如何在上图中访问它们的示例:
// Excel.Workbook wb;
string dmdRegion = wb.ContentTypeProperties["Demand Region"].Value.ToString();
wb.ContentTypeProperties["Demand Region"].Value = "EMEA";
您的示例显示了
工作表
对象中的某些内容,因此,我可能完全错过了这条船。您应该能够调用CustomProperties。如果给定的名称
属性不存在CustomProperty
,请成功添加。通过Name
进行检索从未对工作表有效。CustomProperties
甚至从Excel VBA环境中也不起作用。这是CustomProperties的工作表实现的一个小故障。如果从SmartTag获取CustomProperties集合,则可以按名称索引该集合。链接到的答案显示了如何使用枚举循环按名称进行查找;您尝试过这种技术吗?您应该能够调用CustomProperties。如果CustomProperty
与给定的名称
属性不存在,请成功添加。通过Name
进行检索从未对工作表有效。CustomProperties
甚至从Excel VBA环境中也不起作用。这是CustomProperties的工作表实现的一个小故障。如果从SmartTag获取CustomProperties集合,则可以按名称索引该集合。链接到的答案显示了如何使用枚举循环按名称进行查找;你试过那种技巧吗?