Skip the first line when reading an Excel file

Hello, I am trying to translate an Excel file into my dataGridView file and it has problems with the column name, because the way to format the Excel file consists of two setting cells for the rest of the document. However, the column names are actually on row number 2. How to skip the first row in the file so that the columns in the dataGridView display cell values ​​from the second row?

Current code:

  var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0; data source={0}; Extended Properties=Excel 8.0;", openFileDialog1.FileName);

string query = String.Format("select * from [{0}$]", "Sheet1");
var adapter = new OleDbDataAdapter(query, connectionString);

DataSet ds = new DataSet();

adapter.Fill(ds);

DataTable dt = ds.Tables[0];

techGrid.DataSource = dt;
+2
source share
4 answers

Just as Thit Lwin commented. Delete the first line before setting dt as the data source.

DataRow row = dt.Rows[0];
dt.Rows.Remove(row);
techGrid.DataSource = dt;
+4
source

- Excel , . . OPENQUERY . , tempdb , , .

:

string query = String.Format("select * from [{0}$]", "Sheet1");

:

string query = String.Format("select * from [{0}${1}]", "Sheet1","A2:ZZ");
+9

, , . , , . ConnectionStrings.com:

Provider=Microsoft.ACE.OLEDB.12.0; Data Source=myOldExcelFile.xls; 
Extended Properties="Excel 12.0;HDR=YES";

"HDR = Yes"; , , . "HDR = No;" .

+6

, ,

IEnumerable<DataRow> newRows = dt.AsEnumerable().Skip(numberOfRows);
DataTable  dt2 = newRows.CopyToDataTable();
+2

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


All Articles