如何修复 Excel 自动化中的此异常?

问题描述 投票:0回答:1

正如我在其他地方提到的,我正在编写一个程序来提取和重建 Excel 文件。

直到几天前,一切都很好,但突然 Excel 开始抛出异常,而它从未抛出过异常。

令人费解的是,抛出的异常是“抱歉,我们找不到”。它有可能被移动、重命名或删除吗?’没有任何意义,因为它是在检查文件是否存在后抛出的。

代码中没有任何内容可以保证这种例外。 StackTrace 没有任何用处(如果您感兴趣,它会在问题末尾列出)。

我已经搜索了此错误,虽然我可以找到对此异常的引用,但我找到的建议解决方案都不是用于迭代打开文件,而不是通过自动化。

更奇怪的是 Excel 正常打开文件。

任何人都可以阐明这个问题吗?


堆栈跟踪

这是应用程序爆炸时显示的堆栈跟踪。 调试期间仅显示第一部分,没有内部异常。

Unhandled exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices.COMException: Sorry, we couldn't find SolutionTreePro2.xlam. Is it possible it was moved, renamed or deleted? at Microsoft.Office.Interop.Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru, Object Local, Object CorruptLoad) at ExcelFusion.VbaExtractor.ExtractVbaSourceCode(ExtractOptions options) in C:\Users\pauls\source\repos\Stricklen Pro\StricklenSolutions\ExcelFusion\VbaExtractor.cs:line 33 at ExcelFusion.Program.<>c.<ConfigureExtractHandler>b__2_0(ExtractOptions options) in C:\Users\pauls\source\repos\Stricklen Pro\StricklenSolutions\ExcelFusion\Program.cs:line 102 --- End of inner exception stack trace --- at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Delegate.DynamicInvokeImpl(Object[] args) at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.<InvokeAsync>d__11.MoveNext() in /_/src/System.CommandLine.NamingConventionBinder/ModelBindingCommandHandler.cs:line 87 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.NamingConventionBinder.ModelBindingCommandHandler.Invoke(InvocationContext context) in /_/src/System.CommandLine.NamingConventionBinder/ModelBindingCommandHandler.cs:line 133 at System.CommandLine.Invocation.InvocationPipeline.<>c__DisplayClass4_0.<<BuildInvocationChain>b__0>d.MoveNext() in /_/src/System.CommandLine/Invocation/InvocationPipeline.cs:line 80 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass17_0.<<UseParseErrorReporting>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 501 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass12_0.<<UseHelp>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 393 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass22_0.<<UseVersionOption>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 630 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass19_0.<<UseTypoCorrections>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 562 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<UseSuggestDirective>b__18_0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 536 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass16_0.<<UseParseDirective>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 476 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c.<<RegisterWithDotnetSuggest>b__5_0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 207 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.CommandLine.Builder.CommandLineBuilderExtensions.<>c__DisplayClass8_0.<<UseExceptionHandler>b__0>d.MoveNext() in /_/src/System.CommandLine/Builder/CommandLineBuilderExtensions.cs:line 297

Image depicting the Exception being thrown by Microsoft Excel

c# excel excel-automation excel-exception
1个回答
0
投票
这个问题的根源是你期望计算机做什么和它真正做什么之间存在误解。

测试文件是否存在时,程序使用其当前路径。所有相对路径均参考此路径。

因此,由于路径名(或上例中的

options.ExcelFile

)是相对路径,因此文件是否存在的测试通过了。

然后,打开 Excel 后,Excel 去打开文件(“您”刚刚测试过),Excel 使用

自己的“当前路径”,因此显然无法找到该文件,因为它是一个相对路径。

当我强制使用完整路径时,一切都消失了。

我改变了

var wb = xl.Workbooks.Open(options.ExcelFile);

var xlFilePath = (new FileInfo(options.ExcelFile)).FullName; var wb = xl.Workbooks.Open(xlFilePath);
    
© www.soinside.com 2019 - 2024. All rights reserved.