To read data from a large number of excel files,
ADODB is significantly faster than ComObjGet. In test
ADODB was ~ 16.5 times faster than ComObjGet (6.3 sec vs 103.5 sec).
I created a function - getXcells() - to extract data from Excel sheets using
ADODB. You will need to install the Microsoft Database Engine, a free utility which can be found here:
https://www.microsoft.com/en-us/download/details.aspx?id=54920 if not already installed on your PC.
The function:
Code: Select all
getXcells(datasource, sheet := "sheet1")
{
global colcnt, rowcnt
arr := [], rowcnt := 0
objConnection := ComObjCreate("ADODB.Connection"), objRecordSet := ComObjCreate("ADODB.Recordset")
try objConnection.Open("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" . dataSource . "; Extended Properties='Excel 12.0 xml;HDR=no;IMEX=1';")
catch
{
msgbox,48,, Error! Data could not be retrieved, 2
return
}
try objRecordset.Open("Select * FROM [" Sheet "$]", objConnection, 3, 3, 1) ; adOpenStatic = 3 , adLockOptimistic = 3 , adCmdText = 1
catch
{
msgbox,48,, Error! %Sheet% does not exist, 2
return
}
pFields := objRecordset.Fields
while !objRecordset.EOF
{
row := [], ++rowcnt
Loop, % colcnt := pFields.Count
row[A_Index] := pFields.Item(A_Index-1).value
arr.push(row)
objRecordset.MoveNext
}
objRecordSet.Close()
objConnection.Close()
objRecordSet := ""
objConnection := ""
return arr
}
To get the content of cell A1 out of each of the files in the loop, you can use this:
Code: Select all
Loop, Files, % Path "\Invoice*.xlsx"
lst .= getXcells(A_LoopFileFullPath, "sheet1").1.1 "`n"
msgbox % lst