views:

1083

answers:

5

I am reading data from an Excel 2007 spreadsheet using ADO. Setting up the connection is easy:

Dim ado As ADODB.Connection
Set ado = CreateObject("ADODB.Connection")
ado.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=myFilename.xlsx;Extended Properties=""Excel 12.0 Xml;HDR=NO;IMEX=1"";"
ado.Open

I can call ado.OpenSchema without any trouble on this object. However, when I try to query the data:

Dim rs As ADODB.recordSet
Set rs = ado.Execute("SELECT * FROM [Current Work Load$]")

I simply get a table full of Nulls.

This is mentioned as an issue on the Microsoft Support site - but I have explicitly enabled "Import Mode" (as you can see in the code above - IMEX=1).

+1  A: 

SpreadsheetGear for .NET can read Excel workbooks and enables you to access any cells without the kinds of issues / limatations you can run into with ADO.

You can see live C# & VB samples here and download the free trial here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson
+2  A: 

I've found the ADO connection strings here are unbelievably picky. I've gotten reading the spreadsheets to work but with a slightly different connection string:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + @";Extended Properties="Excel 12.0;IMEX=1";

(I don't have the XML after the Excel 12.0 declaration).

Jeff Siver
Another good resource is http://www.connectionstrings.com
A. Scagnelli
or the HDR=NO; which depends on whether there is one.
Anonymous Type
+1  A: 

As well as using IMEX=1 in the connection string, you need to review a couple of registry keys. For more details, see the excellent (coughs) Daily Dose of Excel blog:

External Data - Mixed Data Types

There is a lot of good stuff in the copious comments.

I really should get around to consolidating those comments into a new post. One day...

onedaywhen
+2  A: 

The Execute method does not return any records as it is for action queries. Your might want to try the OpenRecordset method.

Dim rs As ADODB.recordSet
Set rs = ado.OpenRecordset("SELECT * FROM [Current Work Load$]")
Marcand
A: 

Here is the proper working code http://chetanwarade.wordpress.com/2010/08/18/read-excel-with-ado-net/

cpp