views:

674

answers:

2

[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?

Thanks!

+1  A: 

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

harpo
To clarify, this is because the first row is being interpreted as the column names.
John Gietzen
Thank you very much. :]
03B
A: 

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;'";
Justin Niessner