03B
03B

Reputation: 3

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?

Upvotes: 0

Views: 2590

Answers (2)

Justin Niessner
Justin Niessner

Reputation: 245509

I believe your connection string in this case should be:

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

Upvotes: 0

harpo
harpo

Reputation: 43228

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

Upvotes: 4

Related Questions