In an earlier article, I discussed using ActiveX Data Objects (ADO) to access a Microsoft Access database. A reader commented that ADO can also be used to access data in an Excel worksheet. Here's a brief demonstration...
As usual, we'll use the win32ole library:
require 'win32ole'
Create a new ADODB.Connection object:
connection = WIN32OLE.new('ADODB.Connection')
To open a connection to your Excel workbook, we'll call the Connection object's Open method and pass it a connection string. You can use same the Microsoft Jet driver used for accessing an MS Access database, but we need to append an "Extended Property" to specify that this is an Excel woorkbook:
conn_string = 'Provider=Microsoft.Jet.OLEDB.4.0;'
conn_string << 'Data Source=c:\my_folder\my_workbook.xls;'
conn_string << 'Extended Properties=Excel 8.0;'
connection.Open(conn_string)
Now, we'll create an ADO recordset object:
recordset = WIN32OLE.new('ADODB.Recordset')
When calling the RecordSet object's Open method, pass it your SQL statement and the open connection object. When working with an Excel worksheet as your table, append '$' to the worksheet table name and wrap it in brackets:
recordset.Open("select * from [Sheet1$];", connection)
The Recordset object's GetRows method returns an array of columns (not rows, as you might expect), so we'll use the Ruby array's transpose method to convert it to an array of rows:
data = recordset.GetRows.transpose
Close the Connection object by calling its Close method:
connection.close
There you have it! My thanks to reader Khaoz for the suggestion of using ADO with Excel.
Other articles about working with ADO can be found under the ado label to the right.
As always, feel free to post a comment here or email me with questions, comments, or suggestions.
Thanks for stopping by!