OLEDB Doesn't return the first line of an excel file

I am using Microsoft.ACE.OLEDB.12.0 to connect to excel excel file and get data from it. I write my codes in C # using Visual Studio 2012. here is my code:

public DataTable getData(string fileName, string sheetName) { connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + fileName + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'"; errorCode = ErrorDefinition.ERROR_NOERROR; errorMessage = ""; DataTable dt = new DataTable(); try { string query = "SELECT * FROM [" + sheetName + "]"; OleDbConnection con = new OleDbConnection(connectionString); OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, con); dataAdapter.Fill(dt); } catch (Exception exp) { errorCode = ErrorDefinition.ERROR_OLEDBERROR; errorMessage = exp.Message; } return dt; } 

The problem is that dt does not contain the first line of the specified sheet in the file. What is wrong with it? Any help is appreciated in advanced versions.

+6
source share
2 answers

In the connection string, you use the parameter "HDR=YES" , which means that the first line that you process the Excel OleDb file as the field names of the table returned from the current sheet.

Using "HDR=NO" tells OleDb that the first row contains data, and column names are automatically named progressively. like "F1", "F2", "F3", etc.

+17
source

Try HDR=NO in the connection string

+2
source

All Articles