本文介绍了C#中的Excel跳过第一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用进口的OleDbCommand于DS XLS文件。我遇到的问题是在我的DS它跳跃的第一行的foreach中。我想不通为什么。有什么建议?
cmd.CommandText =的String.Format(SELECT * FROM [{0} $],worksheetName);
conn.Open();VAR适配器=新OleDbDataAdapter的();
变种DS =新的DataSet();
adapter.SelectCommand = CMD;
adapter.Fill(DS);
变种表= ds.Tables [0];的foreach(DataRow的行table.Rows){//我的code的其余部分}
解决方案
从更改连接字符串(如在评论中提及):
字符串CNN =的String.Format(
供应商= Microsoft.ACE.OLEDB.12.0; +
数据源= {0} {1} {2}; +
扩展属性= Excel的8.0;,
fileLocation,文件名,fileExtension);
到
字符串CNN =的String.Format(
供应商= Microsoft.ACE.OLEDB.12.0; +
数据源= {0} {1} {2}; +
扩展属性= Excel的8.0; HDR =否,
fileLocation,文件名,fileExtension);
I'm importing an xls file using OleDbCommand to a ds. Problem I'm having is during the foreach on my ds its skipping for first row. I can't figure out why. Any suggestions?
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 }
解决方案
Change the connection string (as mentioned in comment) from:
string cnn = string.Format(
"Provider=Microsoft.ACE.OLEDB.12.0;" +
"data source={0}{1}{2};" +
"Extended Properties=Excel 8.0;",
fileLocation, fileName, fileExtension);
to:
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);
这篇关于C#中的Excel跳过第一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!