Reading Excel is very slow

John_0025

Active member
Joined
Jun 15, 2004
Messages
32
Im trying to open an Excel workbook and read in the data. It works but it is really slow.

This line is taking all the time
Code:
myRange = myWorksheet.Cells(IntRow, IntColumn)

This is the full code

Code:
    Public Sub New(ByVal oExcel As WorkBookSelection.ExcelWorkBook)        
        Dim myWorksheet As Excel.Worksheet = oExcel.WorkSheets(oExcel.LookUpSheet)
        Dim IntRow As Integer = 2
        Dim myRange As Excel.Range
        Dim IntColumn As Integer

        Do Until myWorksheet.Cells(IntRow, 1).Value = ""
            For IntColumn = 1 To LookUpColumn.MaxColumn
                myRange = myWorksheet.Cells(IntRow, IntColumn)
            Next
            IntRow = IntRow + 1
        Loop

    End Sub

oExcel is defined as

Code:
oExcel = GetObject(, "Excel.Application")

My guess is that it is to do with late binding. Has anyone had this problem
Thanks for any help.
 
Last edited by a moderator:
This works a lot faster, but doesnt explain why the worksheet object was so slow. :confused:

Code:
    Public Sub New(ByVal oExcel As WorkBookSelection.ExcelWorkBook)
        Dim myWorksheet As Excel.Worksheet = oExcel.WorkSheets(oExcel.LookUpSheet)
        Dim IntRow As Integer
        Dim IntColumn As Integer
        Dim oFirstCell As Excel.Range = myWorksheet.Range("A2")
        Dim oLastCell As Excel.Range = myWorksheet.Range("A1").End(Excel.XlDirection.xlDown)
        oLastCell = myWorksheet.Cells(oLastCell.Row, LookUpColumn.MaxColumn)

        Dim myRange As Excel.Range = myWorksheet.Range(oFirstCell, oLastCell)
        Dim Values As System.Array = myRange.FormulaR1C1

        Dim DataProperties(LookUpColumn.MaxColumn) As String
        For IntRow = 1 To Values.GetUpperBound(0)
            For IntColumn = 1 To Values.GetUpperBound(1)
                DataProperties(IntColumn) = CType(Values.GetValue(IntRow, IntColumn), String)
            Next
            DataCollection.Add(key:=AttributeProperties(LookUpColumn.DataName), item:=AttributeProperties)
        Next
    End Sub
 
Back
Top