Often we need to implement reading an Excel workbook via .net applications and because of the ADO.NET advantages, it seems quite an easy task.
Just create a connection string, create a data reader or a Data table and read your excel sheet .!!
Your connection should be something similar to
cnStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & yourfilename & ";Extended Properties=""Excel 8.0;"
But, the main problem is when your Excel sheet contains columns, which are mixed, i.e. your columns values have Numbers and Text both.
In this situation, just by adding IMEX=1 in your connection string , should make life easier.
cn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strServerFileName & ";Extended Properties=""Excel 8.0;IMEX=1"""
Please note that the Extended Properties value is enclosed in quotes.
What happens by using IMEX=1 is, this option tells the connection to honor the registry value ImportMixedTypes = Text, located at HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\
Again if, in some situations, you are supposed to read number from a column and the first few values in the column, say about 10-15 cells are empty, then we face the evil. :)
This is because, when deciding the data type of each and every column, the system will check for the registry key HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows
If the the TypeGuessRows value is, say, 8, and the column's First 10 values are Null, then the column all the values from that column is treated as Null, when everything seems perfect !!!.
So, if you are not getting the values from Excel, first and foremost thing to check and change is the HKLM\Software\Microsoft\Jet\4.0\Engines\Excel\TypeGuessRows value.
Change the TypeGuessRows value as per your requirement and TypeGuessRows value = 0 means all 64465 cells to be scanned before deciding the DataType of the Column.
Hope this helps someone at sometime.. :)
Cheers , Happy coding...
Hemanth Kelkar