vb.net 关闭 Excel.Workbook 时出现 COMException (0x80010108 - RPC_E_DISCONNECTED)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2421188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:45:48  来源:igfitidea点击:

COMException (0x80010108 - RPC_E_DISCONNECTED) When Closing Excel.Workbook

vb.netexcelinteropcom-interopexcel-interop

提问by Steven

When I run the following code, I get the exception below:

当我运行以下代码时,出现以下异常:

''# NOTE: ExcelApp is a Private main form variable
Dim ReportBooks As Excel.Workbooks = ExcelApp.Workbooks
Dim ReportBook As Excel.Workbook = ReportBooks.Open(localFilename)
Dim ReportSheet As Excel.Worksheet = ReportBook.Sheets("Report")

''# Retreive data from sheet

ReleaseCOM(ReportSheet)
ReportBook.Close(True) ''# Error raised here
ReleaseCOM(ReportBook)
ReleaseCOM(ReportBooks)
ERROR:
COMException was unhandled
The object invoked has disconnected from its clients.
(Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))
ERROR:
COMException was unhandled
The object invoked has disconnected from its clients.
(Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))

Note: All data appears to have been retreived correctly.

注意:所有数据似乎都已正确检索。

Please help me diagnose and overcome this error.

请帮助我诊断并克服此错误。

回答by Todd Main

RPC_DISCONNECTED...the dreaded "The object invoked has disconnected from its clients." issue. There are a ton of causes to this, looks like you've covered the global variables issue with Excel.. Can you put the first ReleaseCOM(ReportSheet)below ReportBook(Close)and run it? Also, check out this.

RPC_DISCONNECTED...可怕的“调用的对象已与其客户端断开连接。”问题。造成这种情况的原因有很多,看起来您已经用Excel.. 你能把第一个放在ReleaseCOM(ReportSheet)下面ReportBook(Close)并运行它吗?另外,看看这个

回答by Joe Erickson

I cannot say why it is failing based on the code you are showing.

根据您显示的代码,我无法说出它为什么会失败。

Have you considered switching from Excel Automation using COM Interop to a 3rd party component?

您是否考虑过从使用 COM Interop 的 Excel 自动化切换到第 3 方组件?

SpreadsheetGear for .NETwill let you load Excel workbooks and get values / get formatted text / recalculate formulas / etc... without the troubles associated with COM Interop.

用于 .NET 的 SpreadsheetGear将让您加载 Excel 工作簿并获取值/获取格式化文本/重新计算公式/等...而不会遇到与 COM Interop 相关的麻烦。

You can see live ASP.NET samples hereand download the free trial hereif you want to try it yourself.

你可以看到现场ASP.NET样品这里并下载免费试用这里,如果你想自己尝试一下。

Disclaimer: I own SpreadsheetGear LLC

免责声明:我拥有 SpreadsheetGear LLC