2012-10-31 149 views
1

您好,我試圖將數據從excel導入到Visual Basic變量,但即時通訊出現奇怪的錯誤。我確實添加了對microsoft excel com庫的引用。 (從HRESULT異常:0x80040154的(REGDB_E_CLASSNOTREG))80040154未註冊的類:使用Excel和Visual Studio 2010時出現「Class not registered」錯誤

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 

檢索COM類工廠具有CLSID {00020819-0000-0000-C000-000000000046}組件未能由於下面的錯誤。

回答

4

您不能以這種方式創建工作簿或工作表。 你必須使用Workbooks.Open或Workbooks.Add方法(你做,在ExtraerCostos的第10行)

使用

昏暗的工作簿Excel.Workbook

,並已被沒關係

1

...你宣佈它作爲新的工作簿,但沒有將其設置爲一個值... 試試這個:

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

何pe這有助於幫助

相關問題