First item in excel omitted in results (DataSet, O

2019-08-28 22:44发布

问题:

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

回答1:

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



回答2:

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