vb.net 使用 excel 和 Visual Studio 2010 时出现“未注册类”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13166308/
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
"Class not registered" error when working with excel and visual studio 2010
提问by Diego
Hello i'm trying to import data from an excel to visual basic variables, but im getting an strange error. I did add the reference to microsoft excel com library.
您好,我正在尝试将数据从 excel 导入到可视化基本变量中,但是我遇到了一个奇怪的错误。我确实添加了对 microsoft excel com 库的引用。
Imports Microsoft.Office.Interop.Excel
Module Module1
Sub Main()
ExtraerCostos()
End Sub
Public Sub ExtraerCostos()
Dim numero As String
Dim aux As String
Dim costos(20) As Double
Dim cant As Integer
Dim excelApp As New Microsoft.Office.Interop.Excel.Application
Dim workbook As New Microsoft.Office.Interop.Excel.Workbook ' The error points to this line
Dim sheet As New Microsoft.Office.Interop.Excel.Worksheet
excelApp = CreateObject("Excel.Application")
excelApp.Visible = True
workbook = excelApp.Workbooks.Open("C:\workbook.xls")
sheet = workbook.Worksheets("Factura Detallada")
'Irrelevant code
numero = "111111111"
cant = 12
While numero.Length = 9
cant = cant + 1
End While
For i = 12 To cant
numero = sheet.Cells(i, 1).text
For j = 3 To 22
aux = sheet.Cells(i, j).text
If aux = "-" Then
costos(j - 2) = 0
Else : costos(j - 2) = Convert.ToDouble(aux)
End If
Console.WriteLine(costos(j - 2))
Next
Next
End Sub
End Module
Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
由于以下错误,检索具有 CLSID {00020819-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80040154 类未注册(来自 HRESULT 的异常:0x80040154 (REGDBRET_E)CL)。
回答by Sebastian
you can't create a workbook or worksheet in this way. you have to use the Workbooks.Open or Workbooks.Add method (you do that in line 10 of ExtraerCostos)
您不能以这种方式创建工作簿或工作表。您必须使用 Workbooks.Open 或 Workbooks.Add 方法(您在 ExtraerCostos 的第 10 行中执行此操作)
use
用
Dim workbook as Excel.Workbook
将工作簿变暗为 Excel.Workbook
and already was okay
已经没事了
回答by John Bustos
... You declared it as New workbook, but didn't set it to a value... Try this:
...您将其声明为新工作簿,但未将其设置为值...试试这个:
Dim workbook As Microsoft.Office.Interop.Excel.Workbook = Nothing
Hope this helps
希望这可以帮助

