First item in excel omitted in results (DataSet, O

2019-08-28 22:12发布

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

2条回答
Lonely孤独者°
2楼-- · 2019-08-28 23:11

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

查看更多
男人必须洒脱
3楼-- · 2019-08-28 23:15

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;'";
查看更多
登录 后发表回答