使用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)
它告诉我
并且在例外情况下它显示了我
远程过程调用失败。 (HRESULT异常:0x800706BE)
先谢谢
答案 0 :(得分:0)
基于您的代码的事物对,我相信您的代码是正确的: