C # Excel skip first row?

I am importing an xls file using OleDbCommand in ds. The problem that I encountered is during foreach on my ds of his skipping for the first row. I can’t understand why. Any suggestions?

cmd.CommandText = string.Format("SELECT * FROM [{0}$]", worksheetName); conn.Open(); var adapter = new OleDbDataAdapter(); var ds = new DataSet(); adapter.SelectCommand = cmd; adapter.Fill(ds); var table = ds.Tables[0]; foreach(DataRow row in table.Rows){ // rest of my code } 
+6
source share
3 answers

Change the connection string (as indicated in the comment):

 string cnn = string.Format( "Provider=Microsoft.ACE.OLEDB.12.0;" + "data source={0}{1}{2};" + "Extended Properties=Excel 8.0;", fileLocation, fileName, fileExtension); 

in

 string cnn = string.Format( "Provider=Microsoft.ACE.OLEDB.12.0;" + "data source={0}{1}{2};" + "Extended Properties=Excel 8.0;HDR=No", fileLocation, fileName, fileExtension); 
+13
source

Check the connection string. Most likely, it contains:

 HDR=Yes 

which indicates that the first line is the title

+7
source

Perhaps you told him to skip the first line. I remember a property that sounds like FirstRowIsHeader. I think that if this value is true, then it skips. this can be changed the moment you create your connection.

0
source

Source: https://habr.com/ru/post/892733/


All Articles