Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data is missing while reading excel file using OLEDB

Tags:

c#

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);
like image 983
Vinod kumar Avatar asked Aug 19 '26 06:08

Vinod kumar


1 Answers

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.

like image 88
Hassan Avatar answered Aug 21 '26 20:08

Hassan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!