Reading .XLS file into array

  • Thread starter Thread starter mamabrowsss
  • Start date Start date
M

mamabrowsss

Guest
I have an old app that does this and now have a new app that requires the same so figured I could just use my old code in the new program. But it doesn't work! Been trying for a few days now to figure out and I just don't know what the issue is. Hoping someone could provide some insight. I'll paste the code below and see if I can post an image of the error and line that triggers it. Any help would be greatly appreciated!



Public Function importExcelfileToDataTable(ByVal xlsFileFullName As String) As System.Data.DataTable

Dim returnDT As New System.Data.DataTable()

If File.Exists(xlsFileFullName) Then

' Create new Application.
Dim xlApp As Microsoft.Office.Interop.Excel.Application = New Microsoft.Office.Interop.Excel.Application()

'Open Excel Workbook
Dim xlWorkBook As Microsoft.Office.Interop.Excel.Workbook = xlApp.Workbooks.Open(xlsFileFullName)

'Get sheet the first work sheet Get sheet, if not found return null;
Dim xlWorkSheet As Microsoft.Office.Interop.Excel.Worksheet = xlWorkBook.Worksheets(1) 'get the first worksheet.

Dim startingCol As Integer = xlWorkSheet.UsedRange.Column
Dim numberCol As Integer = xlWorkSheet.UsedRange.Columns.Count
Dim startingRow As Integer = xlWorkSheet.UsedRange.Row
Dim numberRow As Integer = xlWorkSheet.UsedRange.Rows.Count

'Get range
Dim xlRange As Microsoft.Office.Interop.Excel.Range = xlWorkSheet.UsedRange

'Load all cells into Array
Dim arrayRanges(,) As Object = xlRange.Value 'This line loads the range of the excel file into an array

xlWorkBook.Close()
xlApp = Nothing
xlRange = Nothing
xlWorkSheet = Nothing

'import into Datatable by converting the object array to a table
returnDT = convertToDataTable(arrayRanges)

End If

End Function


Here's the error:

1422031.png

Continue reading...
 
Back
Top