BrianPeek.com
A Compendium of Random Uselessness
Excel and OLEDB in .NET
While attempting to do some data merging across several Excel spreadsheets, I found some interesting behavior in how OLEDB determines data types of columns with an Excel source.

For me, the issue manifested itself with a column that looked like the following (shortened for brevity's sake):

10132
10133
10134D
10134
10135


By default, Excel looks at the first 8 rows of each column to determine the type.  If most of the items are numeric, it will create the column as an int or double type.  The problem is, if the column also contains text data, like the one above (10134D), the column can't be converted to a numeric type and OLEDB will just drop the value with no error or explanation.

With the above data, since 4 out of 5 are numeric, OLEDB turns the column into a numeric type, and the 3rd row's column of that data would be NULL.

I tried several ways of fixing the issue but found that the only solution that worked was to set the IMEX property in the connection string to 1 in the Extended Properties section like so:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=blah.xls;Extended Properties="Excel 8.0;IMEX=1;"

An additional extended property you might find useful is "HDR=1", which tells the driver that the first row of the spreadsheet contains text data as a header and not column data.

Apparently this tells the OLEDB driver for Excel that the data is "intermixed" and that it should convert any intermixed data to the type specified in the registry at:

HKLM\SOFTWARE\Microsoft\Jet\4.0\Engines\Excel\ImportMixedTypes

By default, this is "Text", which is exactly what I wanted.  You'll also notice a key in there named "TypeGuessRows".  This is the number of rows that Excel will use to determine the data-type of the column, the default being 8 as described above.  You may need to tweak this value to a number greater than the row at which your data changes in the column you want as a string type.

Anyway, it's probably useless to most, but it took me a bit of time and research to figure out so I thought I'd pass it along.


Posted Tue, Apr 18 2006 5:01 PM by Brian Peek
Filed under:

Comments

Tom wrote re: Excel and OLEDB in .NET
on Sat, May 20 2006 10:24 PM
Thanks for that tip - it answered a question that's been perplexing me!  
Rich Frank wrote re: Excel and OLEDB in .NET
on Fri, Aug 18 2006 11:38 AM
Excel looks at the first 8 rows...

Thanks for the information.  I continue to bt dumbfounded how quirky and undocumented all this stuff is.
Erik wrote re: Excel and OLEDB in .NET
on Thu, Oct 12 2006 1:57 PM
Thanks, very useful. See also the following link for a more in-depth discussion:

http://support.microsoft.com/kb/194124/EN-US/
"PRB: Excel Values Returned as NULL Using DAO OpenRecordset"
Mogens Nielsen wrote re: Excel and OLEDB in .NET
on Tue, Oct 24 2006 9:02 AM
Thanks - this one really helped me out!
Julian wrote re: Excel and OLEDB in .NET
on Wed, Dec 27 2006 9:53 AM
There's more to IMEX.  I had a spreadsheet with 10 rows of null preamble then numeric values.  These would not display until I set IMEX=1.  I then wanted to write to another XL template using an INSERT statement and I kept getting the "Operation must use an updateable query" error.   Turns out this had nothing to do with permissions.  Instead, for that query I had to make sure I removed the IMEX extended property and then the query worked.
Ahmed ElAraby wrote re: Excel and OLEDB in .NET
on Tue, Nov 6 2007 6:10 AM

Just add an empty row after the header to specify the type of the row, ex: numeric, datetime...etc.

then insert your rows. your inserted rows will be after that empty row. you can then reference these rows in other sheets.

Cluebat-man to the rescue wrote Excel and OleDb stupidity
on Fri, Feb 29 2008 4:13 PM

This afternoon I was working on a Windows Forms app for the finance guys, to help them allocate costs

Marcus Andersson wrote re: Excel and OLEDB in .NET
on Sat, Nov 15 2008 11:41 AM

For me, this information wasn't useless :-)

It solved a problem I've been experiencing when importing data from an Excel-file and generate it as XML. The article-number-column had mixed values which made the provider set is as an empty string. However, IMEX=1 did the trick.

Thanks!

Per wrote re: Excel and OLEDB in .NET
on Mon, Dec 1 2008 6:01 AM

I could kiss you Brian.

At first I couldn't get it to work, but then I read the end of the article about the "TypeGuessRows". I set it to 10k and everything worked perfectly, thanks alot.

Btw, I timed the operation with default value, and 10k value, my spreadsheet has 1085 rows, and the first letter is at 1081. Couldn't find any significant increase in execution time

Marv Doolin wrote re: Excel and OLEDB in .NET
on Thu, Jul 16 2009 11:22 AM

Thanks! This solved several problems in one swoop!

Merine wrote re: Excel and OLEDB in .NET
on Tue, Jan 26 2010 10:50 PM

Thanks a lot, you saved my life... :D

Add a Comment

(required)  
(optional)
(required)  
Remember Me?