ADO Example Excel: Difference between revisions
Jump to navigation
Jump to search
(Created page with "Global $oADOConnection = ObjCreate("ADODB.Connection") ; Create a connection object Global $oADORecordset = ObjCreate("ADODB.Recordset") ; Create a recordset object Global $sF...") |
No edit summary |
||
Line 1: | Line 1: | ||
= Read XLS file and display the cells of a range = | |||
The following example | |||
* Reads the cells of a range of an XLS file and displays two columns of each row | |||
* The Excel worksheet has no header row | |||
Example AutoIt script: | |||
<syntaxhighlight lang="autoit"> | |||
Global $oADOConnection = ObjCreate("ADODB.Connection") ; Create a connection object | Global $oADOConnection = ObjCreate("ADODB.Connection") ; Create a connection object | ||
Global $oADORecordset = ObjCreate("ADODB.Recordset") ; Create a recordset object | Global $oADORecordset = ObjCreate("ADODB.Recordset") ; Create a recordset object | ||
Line 9: | Line 15: | ||
While Not .EOF ; repeat until End-Of-File (EOF) is reached | While Not .EOF ; repeat until End-Of-File (EOF) is reached | ||
; Write the content of all fields to the console separated by | by processing the fields collection | ; Write the content of all fields to the console separated by | by processing the fields collection | ||
ConsoleWrite("Process the fields collection: | ConsoleWrite("Process the fields collection: ") | ||
For $oField In .Fields | For $oField In .Fields | ||
ConsoleWrite($oField.Value & "|") | ConsoleWrite($oField.Value & "|") | ||
Next | Next | ||
ConsoleWrite(@CR) | ConsoleWrite(@CR) | ||
; Write a second line by accessing all fields of the collection by item number | ; Write a second line by accessing all fields of the collection by name or item number | ||
ConsoleWrite("Process the fields by item number: " & .Fields( | ConsoleWrite("Process the fields by name/item number: " & .Fields("F1").Value & "|" & .Fields(1).Value & "|" & @CR) | ||
.MoveNext ; Move To the Next record | .MoveNext ; Move To the Next record | ||
WEnd | WEnd | ||
Line 23: | Line 29: | ||
$oADOConnection.Close ; Close the connection | $oADOConnection.Close ; Close the connection | ||
$oADOConnection = 0 ; Release the connection object | $oADOConnection = 0 ; Release the connection object | ||
</syntaxhighlight> | |||
Result: | |||
Process the fields collection: F1|F2| | |||
Process the fields by name/item number: F1|F2| | |||
Process the fields collection: F1.1|F2.1| | |||
Process the fields by name/item number: F1.1|F2.1| |
Revision as of 09:56, 8 January 2014
Read XLS file and display the cells of a range
The following example
- Reads the cells of a range of an XLS file and displays two columns of each row
- The Excel worksheet has no header row
Example AutoIt script:
Global $oADOConnection = ObjCreate("ADODB.Connection") ; Create a connection object
Global $oADORecordset = ObjCreate("ADODB.Recordset") ; Create a recordset object
Global $sFilename = @ScriptDir & "\ADO_Example_Excel.xls"
Global $sADOConnectionString = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' & $sFilename & ';Extended Properties="Excel 8.0;HDR=No"'
$oADOConnection.Open($sADOConnectionString) ; Open the connection
Global $sADOSQL = "Select F1,F2 FROM [Sheet1$A1:B2]" ; Select all records and all fields
$oADORecordset.Open($sADOSQL, $oADOConnection) ; Issue the SQL query
With $oADORecordset
While Not .EOF ; repeat until End-Of-File (EOF) is reached
; Write the content of all fields to the console separated by | by processing the fields collection
ConsoleWrite("Process the fields collection: ")
For $oField In .Fields
ConsoleWrite($oField.Value & "|")
Next
ConsoleWrite(@CR)
; Write a second line by accessing all fields of the collection by name or item number
ConsoleWrite("Process the fields by name/item number: " & .Fields("F1").Value & "|" & .Fields(1).Value & "|" & @CR)
.MoveNext ; Move To the Next record
WEnd
EndWith
$oADORecordset.Close ; Close the recordset
$oADORecordset = 0 ; Release the connection object
$oADOConnection.Close ; Close the connection
$oADOConnection = 0 ; Release the connection object
Result:
Process the fields collection: F1|F2| Process the fields by name/item number: F1|F2| Process the fields collection: F1.1|F2.1| Process the fields by name/item number: F1.1|F2.1|