使用excel和visual studio 2010时出现“Class not registered”错误

你好,我试图从Excel中导入数据到可视化的基本variables,但即时通讯一个奇怪的错误。 我没有添加对微软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 

检索具有CLSID {00020819-0000-0000-C000-000000000046}的组件的COM类工厂失败,原因如下:80040154类未注册(exception来自HRESULT:0x80040154(REGDB_E_CLASSNOTREG))。

您不能以这种方式创build工作簿或工作表。 您必须使用Workbooks.Open或Workbooks.Add方法(您可以在ExtraerCostos的第10行中执行此操作)

使用

昏暗的工作簿作为Excel.Workbook

而且已经很好了

…您将其声明为新的工作簿,但没有将其设置为值…试试这个:

 Dim workbook As Microsoft.Office.Interop.Excel.Workbook = Nothing 

希望这可以帮助