Error reading data from an Excel Spreadsheet

I use the following code snippet to read an Excel spreadsheet.
	private const string EXCEL_CONNECTION_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=Yes'";
	string connectionString = string.Format(EXCEL_CONNECTION_STRING, this.WorkingFilename);
    using (OleDbConnection excelConnection = new OleDbConnection(connectionString))
        dt = new DataTable();
        using (OleDbCommand excelCommand = new OleDbCommand("SELECT * FROM [BALANCE$]", excelConnection))
            using (OleDbDataAdapter da = new OleDbDataAdapter())
                da.SelectCommand = excelCommand;

Open in new window

Most of the time it works fine but today (for some reason) the file that was provided had an anomaly. Some of the items in the Amount column were numbers stored as text and when I executed the above code it didn't populate column 3 properly in those cases. I was able to work around the issue by converting the text to numbers manually in Excel but I'd like the code to be able to handle it better. What changes can I make so that when it encounters this issue it still imports the numeric data properly?

I have attached a sample Excel spreadsheet that can be used with the above code to demonstrate the problem.
LVL 21
Russ SuterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieVBA ExpertCommented:
Don't know if it's relevant, and I've not had a chance to test the code, but why are you specifying Excel8.0 in the connection string?

The file you attached is in xlsx format so I thought you would use Excel 12.0 Xml.

Also, you don't have IMEX=1 in the Extended properties section, having that can help when dealing with mixed data types.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
[ fanpages ]IT Services ConsultantCommented:
(Picking-up this question via the MS-Excel Topic Area/Channel)

I would like to reiterate what Norie suggested:

Use ;IMEX=1 & Excel 12.0 within your Connection String:

private const string EXCEL_CONNECTION_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0;HDR=Yes;IMEX=1'";

(The single quotation mark around the Extended Properties criteria may not be strictly necessary.  It is not required when using ADODB recordsets of this nature from within an MS-Excel/VBA environment, for instance).

private const string EXCEL_CONNECTION_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;HDR=Yes;IMEX=1";

I would also suggest that as you are using SELECT * within your SQL statement, you could always explicitly list the columns & convert the [Amount] value to a (Currency) numeric value within the recordset creation.

Alternatively, you could replace all the currency symbol ($) characters in advance (search/replace "$" to "") using the "Excel.Application" automation object, or via another method from your own source/programming environment.

PS. I am presuming that the {0} within the Data Source={0} parameter is replaced with the (fully-qualified folder-inclusive) name of the workbook elsewhere in your code:

string connectionString = string.Format(EXCEL_CONNECTION_STRING, this.WorkingFilename);
Russ SuterAuthor Commented:
Worked perfectly. Not sure how I missed the Excel 8.0 part in the connection string. I don't think that was the solution but it's nice to keep things tidy. Adding IMEX=1 did the trick though. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.