使用introp将其打开到vb.net后Excel崩溃

时间:2017-02-15 12:25:24

标签: excel vb.net

使用introp

打开vb.net后Excel崩溃

Followig是我用来从excel

导入数据的代码
    Dim misValue As System.Reflection.Missing
    Dim tbl As New System.Data.DataTable
    Dim xlApp As nExcel.Application
    Dim xlWorkBook As nExcel.Workbook
    Dim xlWorkSheet As nExcel.Worksheet

    xlApp = New nExcel.Application
    Try
        xlWorkbook = xlApp.Workbooks.Open(sFilePath)


        Dim issheetext As Boolean
        Dim sheet As nExcel.Worksheet
        For Each sheet In xlWorkbook.Sheets
            If sheet.Name.Equals(sSheetname) Then
                issheetext = True
                Exit For
            End If
        Next
        If issheetext Then
            xlWorkSheet = DirectCast(xlWorkbook.Sheets(sSheetname), nExcel.Worksheet)

            ' get range object
            Dim myRange As nExcel.Range = DirectCast(xlWorkSheet.get_Range("A1:BZ102451"), nExcel.Range)

            For i As Integer = 0 To myRange.Columns.Count
                Dim cellval As Object
                cellval = DirectCast(myRange.Cells(1, i + 1), Object)
                tbl.Columns.Add(cellval.ToString(), cellval.GetType())
            Next
            For i As Integer = 0 To myRange.Rows.Count - 2
                tbl.Rows.Add()

                For j As Integer = 0 To myRange.Columns.Count - 1
                    Dim CellValue As Object = DirectCast(myRange.Cells(2 + i, j + 1).Value2, Object)
                    tbl.Rows(i)(j) = CellValue
                Next
            Next
        End If
    Catch ex As Exception
    Finally
        Try
            releaseObject(xlWorkSheet)
            releaseObject(xlWorkbook)
            releaseObject(xlApp)
        Catch
        End Try


    End Try

    Return tbl

但调试器越过xlWorkbook = xlApp.Workbooks.Open(sFilePath)

它告诉我

Excel Crash Report

并且在例外情况下它显示了我

  

远程过程调用失败。 (HRESULT异常:0x800706BE)

先谢谢

1 个答案:

答案 0 :(得分:0)

基于您的代码的事物对,我相信您的代码是正确的:

  • 请验证文件路径 sFilePath
  • 任何后台弹出窗口阻止Excel启动
  • 从任务管理器中杀死所有Excel并重新启动计算机
  • 在“窗口运行”中,键入Excel -r并按Enter键(它将Excel重置为默认值)