I'm trying to write an import function for getting data out of an excel file. How I currently do it is as follows:
Private Sub ReadExcel(ByVal childform As PhoneDiag.frmData, ByVal FileName As String)
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
xlWorkBook = xlApp.Workbooks.Open(FileName)
xlWorkSheet = xlWorkBook.Worksheets(1)
Dim columnrange = xlWorkSheet.Columns
Dim therange = xlWorkSheet.UsedRange
''Add rows by column
For rCnt = 2 To therange.Rows.Count
Dim rowArray(therange.Columns.Count) As String
For cCnt = 1 To therange.Columns.Count
Dim Obj = CType(therange.Cells(rCnt, cCnt), Excel.Range)
Dim celltext As String
celltext = Obj.Value.ToString
rowArray((cCnt - 1)) = celltext
Next
childform.datagridSample.Rows.Add(rowArray)
Next
'' make sure we close the excel.exe service after use
xlWorkBook.Close()
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet)
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
End Sub
The problem is, of course, is that it runs horribly. From what I can gather, it more than likely comes down to this line:
Dim Obj = CType(therange.Cells(rCnt, cCnt), Excel.Range)
All I need is the text from the cells, not to create an object for each cell (and then not send them to garbage collection). Is there an easier way of just getting the text?
Ideally, if I can get a method for getting the text values of the cell, I'd like to get the multiple rowArray()'s added to a master array and update the program's values later.
If you see any other performance tips, let me know. It would be much appreciated. =b
EDIT: I also realize that I have two options if I were to create a master array, say mArr, to hold all the data. Would it be better performance-wise to have mArr to be large and the sub arrays small, or mArr to be small and the sub arrays to hold more of the information?
I ask because the files that will be imported will have more rows than columns, so I was wondering if there was any "set" way of doing it.