3

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);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Y2theZ
  • 10,162
  • 38
  • 131
  • 200
  • Even I encountered such problems. You can refer : http://stackoverflow.com/questions/6913497/unknown-problem-while-exporting-excel-to-system-datatable and http://stackoverflow.com/questions/1941083/problems-reading-in-an-excel-file-in-c also this http://stackoverflow.com/questions/6913497/unknown-problem-while-exporting-excel-to-system-datatable Make Sure your connections are proper http://www.connectionstrings.com/excel – Pratik Sep 07 '11 at 13:49

3 Answers3

6

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

Y2theZ
  • 10,162
  • 38
  • 131
  • 200
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?

Community
  • 1
  • 1
Daniel B
  • 2,877
  • 18
  • 18
  • Thank you for your answer. But I know that the first row is a header and did not count it. it is filling the header correctly but the next 3 rows are not filled. And no there is no difference in their content – Y2theZ Sep 06 '11 at 14:55
0

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

alexey
  • 783
  • 1
  • 7
  • 19