.net - 使用 excel 和 Visual Studio 2010 时出现 "Class not registered"错误

标签 .net vb.net visual-studio-2010 excel excel-interop

您好,我正在尝试将数据从 Excel 导入到 Visual Basic 变量,但我遇到了一个奇怪的错误。我确实添加了对 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

由于以下错误,检索具有 CLSID {00020819-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败:80040154 类未注册(HRESULT 异常:0x80040154 (REGDB_E_CLASSNOTREG))。

最佳答案

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

利用

将工作簿变暗为 Excel.Workbook

并且已经好了

关于.net - 使用 excel 和 Visual Studio 2010 时出现 "Class not registered"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13166308/

相关文章:

c# - 在 silverlight 和 WPF 中实现多语言应用程序的最佳方式

java - 网络服务 @.net 或 java

MySQL 键有点奇怪

mysql - 是否有设置或某些问题会导致 MySql 在查询之间等待 1 秒?

.net - 确保每个子 UserControl 创建自己的 ViewModel 实例

c++ - 在 c++ 中,vs2010 的 typeid 名称与 cocoa 的名称不同。为什么?哪一个是对的?

.net - 在后台线程中调用 LocalReport.Render

c# - 从 .NET 程序集加载项目设置

c# - 将我的 sql 与 Visual Studio 2010 连接

visual-studio-2010 - 是否可以通过 Microsoft 测试管理器安排自动化测试?