c#Excel跳过第一行?

Sco*_*ott 6 c# asp.net excel

我正在使用OleDbCommand将xls文件导入ds.我遇到的问题是在我的ds的foreach期间跳过第一排.我无法弄清楚为什么.有什么建议?

cmd.CommandText = string.Format("SELECT * FROM [{0}$]", worksheetName);
conn.Open();

var adapter = new OleDbDataAdapter();
var ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
var table = ds.Tables[0];

foreach(DataRow row in table.Rows){ // rest of my code }
Run Code Online (Sandbox Code Playgroud)

Bri*_*ntz 14

从以下位置更改连接字符串(如注释中所述):

string cnn = string.Format( 
    "Provider=Microsoft.ACE.OLEDB.12.0;" +
    "data source={0}{1}{2};" +
    "Extended Properties=Excel 8.0;", 
    fileLocation, fileName, fileExtension);
Run Code Online (Sandbox Code Playgroud)

至:

string cnn = string.Format( 
    "Provider=Microsoft.ACE.OLEDB.12.0;" +
    "data source={0}{1}{2};" +
    "Extended Properties=Excel 8.0;HDR=No", 
    fileLocation, fileName, fileExtension);
Run Code Online (Sandbox Code Playgroud)


Jul*_*usz 7

检查连接字符串.最有可能包含:

HDR=Yes
Run Code Online (Sandbox Code Playgroud)

表示第一行是标题

  • ......如果是,请将其更改为"HDR =否":) (2认同)