OleDb Connection not reading all the rows from excel file

二次信任 提交于 2019-12-13 14:32:23

问题


I am using an OleDb in C# connection to read data from an Excel sheet. And fill it in a Datatable.

The sheet contains 275 rows and 27 columns. After I read it, Rows 1,2 and 3 are empty. All the other rows are filled correctly.

Anyone have an idea on the problem?

Here is my code:

string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                "Data Source=" + "C:/Sheets/DataSheet.xls" + ";Extended Properties=Excel 12.0;";

objConn = new OleDbConnection(connString);

string Query = "SELECT * FROM [Sheet1$]";
OleDbCommand objCmd = new OleDbCommand(Query, objConn);

DataTable Table = new DataTable();
objAdapter1.SelectCommand = objCmd;
objAdapter1.Fill(Table);

回答1:


The problem was that my sheet contained mixed data and it was only reading numbers. The solution is to specify

Properties=\"Excel 12.0;IMEX=1\";"

IMEX=1 allow the reader to import all data not only numbers




回答2:


This post has some information on the first row being treated as a header, unless you specify HDR=NO in the connection string.

I am not sure why the 2nd and 3rd rows might be skipped however; is there any difference in their contents that you can see?




回答3:


I just added new row to upper row and it appears in dataset. So rows orders changing solved problem in my case



来源:https://stackoverflow.com/questions/7321266/oledb-connection-not-reading-all-the-rows-from-excel-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!