Search code examples
c#.netexceldatasetoledb

First item in excel omitted in results (DataSet, OleDB)


[Sample.xlsx]

Column 0, Row 0 = "ItemA"    
Column 0, Row 1 = "ItemB"    
Column 0, Row 2 = "ItemC"    
Column 0, Row 3 = "ItemD"

[Application]

DataSet dsData = new DataSet();    
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.xlsx;Extended Properties='Excel 12.0;'";    
OleDbDataAdapter daGetExcel = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", strConn);    
daGetExcel.Fill(dsData);        

foreach (DataRow row in dsData.Tables[0].Rows) 
{ 
    lbExcelData.Items.Add(row[0].ToString()); 
}

lbExcelData is a ListBox control on the form.

[RESULTS]

"ItemB", "ItemC", "ItemD"

[PROBLEM]

Why is the first item, "ItemA", being ignored?


Solution

  • For Excel, set HDR=NO in the Extended Properties setting of the connection string.

    "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Sample.xlsx;Extended Properties='Excel 12.0;HDR=NO'"

    http://connectionstrings.com/excel-2007