简体   繁体   中英

Data is missing while reading excel file using OLEDB

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);

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.

将IMEX = 1添加到连接字符串,如下所示:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM