在位置0没有行:从数据集中获取值在c#中的文本框
我正在尝试从Excel中的单元格到C#中的文本框的价值。 我已经使用数据集来从Excel中获取值,但是当从数据集到文本框中取值时,会给出错误
位置0没有排
我也尝试使用Item Array,但没有用。 下面是我的代码:
DataSet dsloc = new DataSet(); OleDbDataAdapter daloc = new OleDbDataAdapter(); string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtfilepath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";"; OleDbConnection con = new OleDbConnection(connStr); string strloc = "select * from [sheet1$A5:A5]"; OleDbCommand cmdloc = new OleDbCommand(strloc, con); con.Open(); dsloc.Clear(); daloc.SelectCommand = cmdloc; daloc.Fill(dsloc); txtloc.Text = dsloc.Tables[0].Rows[0]["Location"].ToString();
我努力了
DataRow drow; drow = dsloc.Tables[0].Rows[0]; txtloc.Text = drow.ItemArray.GetValue(1).ToString();
也
txtloc.Text = dsloc.Tables[0].Rows[0][0].ToString();
我的数据集显示它有价值。
但是这些工作都没有。 请指教。 谢谢
找出解决办法,来自excel的值实际上是数据表的列的名称,因为我可以在截图中看到,所以我只需要获取列名。
DataTable dtloc = new DataTable(); string connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + txtfilepath.Text + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";"; string strloc = "select * from [sheet1$A5:A5]"; OleDbCommand cmdloc = new OleDbCommand(strloc, con); con.Open(); daloc.SelectCommand = cmdloc; string location = dtloc.Columns[0].ColumnName.ToString();
也使用数据表而不是数据集。