问题描述
我正在开发一个需要从Excel导入数据的应用程序.
我的解决方案是使用 Microsoft.office.Interop.Excel.
但是我在调试时收到此错误:
下面是我的代码:
导入Excel = Microsoft.office.Interop.Excel私有子BExcel1_Click(发送者作为对象,e作为EventArgs)处理BExcel1.ClickOpenFileDialog1.Filter ="Excel文件| * .xlsx; * .xls; * .xlsm"如果(OpenFileDialog1.ShowDialog()= DialogResult.OK)然后ExcelPath1.Text = OpenFileDialog1.FileName万一Dim XlApp作为新的Excel.ApplicationDim XlWorkBook作为Excel.WorkbookDim XlWorkSheet作为Excel.WorksheetXlWorkBook = XlApp.Workbooks.Open(ExcelPath1.Text)结束子
我已经在Google上找到了一些解决方案(如下所示),但没有解决:
- 我已经修复了办公室
- 我已经修复了Visual Studio
- 我已使用注册表编辑器检查
Computer \ HKEY_CLASSES_ROOT \ TypeLib \
,但是在00020813-0000-0000-C000-000000000046
下,我只有一个1.9版,好像不是两个版本之间的冲突?
有解决此问题的主意吗?
VS版本:2017社区Excel版本:2016Microsoft.Office.Interop.Excel版本:15.0.0.0
首先,尝试使用/ResetUserData
命令行参数运行Visual Studio.在
I am working on an app which needs to import data from Excel.
My solution is using Microsoft.office.Interop.Excel.
But I get this error when I debug:
Below is my code:
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
I have googled to find some solutions (as following), but they didn't work out:
- I have repaired Office
- I have repaired Visual Studio
- I have used the registry editor to check the
Computer\HKEY_CLASSES_ROOT\TypeLib\
, but under the00020813-0000-0000-C000-000000000046
, I got only one version 1.9, it seems not like the conflict between two version?
Any ideas to fix this problem?
VS version:2017 community
Excel version:2016
Microsoft.Office.Interop.Excel version:15.0.0.0
First of all, try to run Visual Studio with the /ResetUserData
command line argument. Read more about that in the Error "Unable to cast COM object..." when exporting to Microsoft Excel from Team Explorer 2008 article.
Obviously you are trying to connect to a wrong Excel version. Looks like you have some extra windows registry keys left after uninstalling an old version of Office or vice versa. Anyway, take a look at the How to solve "Unable to cast COM object of type Microsoft.Office.Interop.Excel.ApplicationClass’ to interface type ‘Microsoft.Office.Interop.Excel._Application’" blog post which describes exactly the same issue. Basically you need to find a wrong entry in the windows registry and then delete it.
BTW When you add a new COM reference to the project a missed PIA is generated automatically (if it doesn't exist any longer). So, that is a possible way to go. Also you may try to embed interop types into your own assembly like the following screenshot shows:
这篇关于VB:加载类型库/DLL时出错.(来自HRESULT的异常:0x80029C4A(TYPE_E_CANTLOADLIBRARY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!