excel - VB : Error loading type library/DLL.(HRESULT 异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY)

标签 excel vb.net office-interop

我正在开发一个需要从 Excel 导入数据的应用。

我的解决方案是使用 Microsoft.office.Interop.Excel。

但是我调试的时候出现了这个错误:

Message=Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ApplicationClass' to interface type 'Microsoft.Office.Interop.Excel._Application'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{000208D5-0000-0000-C000-000000000046}' failed due to the following error: Error loading type library/DLL. (Exception from HRESULT: 0x80029C4A (TYPE_E_CANTLOADLIBRARY))

下面是我的代码:

Imports Excel = Microsoft.office.Interop.Excel

Private Sub BExcel1_Click(sender As Object, e As EventArgs) Handles BExcel1.Click
    OpenFileDialog1.Filter = "Excel Files|*.xlsx; *.xls; *.xlsm"
    If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
        ExcelPath1.Text = OpenFileDialog1.FileName
    End If

    Dim XlApp As New Excel.Application
    Dim XlWorkBook As Excel.Workbook
    Dim XlWorkSheet As Excel.Worksheet
    XlWorkBook = XlApp.Workbooks.Open(ExcelPath1.Text)

 End Sub

我用谷歌搜索了一些解决方案(如下),但没有成功:

  1. 我已经修好了 Office
  2. 我已经修复了 Visual Studio
  3. 我已经使用注册表编辑器检查了 Computer\HKEY_CLASSES_ROOT\TypeLib\,但是在 00020813-0000-0000-C000-000000000046 下,我只有一个1.9版本,两个版本好像没有冲突吧?

有解决这个问题的想法吗?

VS version:2017 community

Excel version:2016

Microsoft.Office.Interop.Excel version:15.0.0.0

最佳答案

首先,尝试使用 /ResetUserData 命令行参数运行 Visual Studio。在 Error "Unable to cast COM object..." when exporting to Microsoft Excel from Team Explorer 2008 中阅读更多相关信息文章。

显然,您正在尝试连接到错误的 Excel 版本。看起来您在卸载旧版本的 Office 后还剩下一些额外的 Windows 注册表项,反之亦然。无论如何,看看How to solve “Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’”描述完全相同问题的博客文章。基本上,您需要在 Windows 注册表中找到错误的条目,然后将其删除。

顺便说一句,当您向项目添加新的 COM 引用时,会自动生成丢失的 PIA(如果它不再存在)。所以,这是一个可行的方法。您也可以尝试将互操作类型嵌入到您自己的程序集中,如下面的屏幕截图所示:

enter image description here

关于excel - VB : Error loading type library/DLL.(HRESULT 异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210215/

相关文章:

sql - 追加查询,跳过重复条目(重复项不是主键)

vb.net - 需要有关 VB.Net 多线程选项的建议

php - 使用 PHP Codeigniter 创建 Excel 文件

python xlrd从excel文本单元格接收 float

regex - Vb.net Regex - 从字符串中删除 html 标签

c# - 从 Outlook 2007 加载项修改 ActiveInlineResponseWordEditor

c# - ppt保存幻灯片时保留源模板

c# - microsoft.interop.excel 格式化单元格

excel - 如何检查单元格的当前值是否在所有先前单元格的某个值之内

java - 在 VB.NET 应用程序的窗口中运行 JAR 应用程序 (Java)?