3

I am using OLEDB to read excel file into datatable. But the problem is, some values are missing(Empty). In my excel sheet one column datatype is General, it has mixed values like string and integers. Most of the cell values are integers. Why OLEDB is skipping string values.

OleDbConnection connection = new OleDbConnection();

connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + "; Extended Properties=\"Excel 12.0;IMEX=1\";";
OleDbCommand myAccessCommand = new OleDbCommand();

myAccessCommand.CommandText = "Select * from [" + sheetName + "]";

OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myDataAdapter.Fill(myDataSet);
1
  • We still need to see how you actually retrieve the data out of the sheet Commented Mar 21, 2014 at 7:34

2 Answers 2

5

Check following link and see points under "RESOLUTION": http://support.microsoft.com/kb/194124

Please see point 2 NOTE.

Setting IMEX=1 is entirely dependent on your registry settings. By default, first 8 rows are checked to determine the data type. IMEX=1 can give unpredictable behaviors, such as skipping string values. There is also one small workaround for this problem. Just add single quote (') before every cell value in excel. Every cell will be treated as string.

Sign up to request clarification or add additional context in comments.

2 Comments

Add a little explanation also.
In Excel set cell format to 'Text' instead of 'General' will also work.
0

Add IMEX=1 to the connection string as below:

string con = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" + @"Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fileName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.