Tuesday, May 23, 2006

VS: Excel datasource reads values as DBNull

When using a connectionstring to connect to a Excel sheet like this:

SELECT * FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\test\xltest.xls;Extended Properties=Excel 8.0')...[Customers$]


you can create a dataset based on the excel sheet.

However, using this connectionstring with mixed colums (columns that contain numeric values and tekst values) the provider will think the column is numeric and won't read the tekstual values.

To solve this problem you have to add an extended property IMEX to the connectionstring, tellling the provider to treat mixed colums (default) as string.
This way all the values in the columns will be read:

SELECT * INTO XLImport3 FROM OPENDATASOURCE('Microsoft.Jet.OLEDB.4.0',
'Data Source=C:\test\xltest.xls;Extended Properties=''Excel 8.0;IMEX=1''')...[Customers$]