积极答复者
C#如何从Excel中提取数据到DataGridView中某一列

问题
-
如题```
- 已移动 Sheng Jiang 蒋晟Moderator 2009年11月23日 23:12 Office开发 (发件人:Visual C#)
答案
全部回复
-
这是偶写的一段从Excel中提取数据的代码:
private int GetExcelCon() { string connection = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = C:\\Documents and Settings\\yangjh\\桌面\\sample.xls;Extended Properties=Excel 8.0"; OleDbConnection thisconnection = new OleDbConnection(connection); string command = "select * from [sheet1$]"; thisconnection.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(command, thisconnection); DataSet ds = new DataSet(); adapter.Fill(ds, "[sheet1$]"); thisconnection.Close(); foreach (DataRow Row in ds.Tables[0].Rows) { //int RowCount=ds.Tables[0].Rows.Count;//?这段代码不知道该如何写,从DataTable中提取第0列作为参数 } }
-
这是偶调用DataTable第0列的参数,执行SQL存储过程的代码:
private DataTable SampleData { get { string connection = "Server=192.100.3.3;Datasource=AIS20090330165131;uid=sa;pwd=;"; SqlConnection thisconntion = new SqlConnection(connection); SqlCommand cmd = thisconntion.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "t_StoreAndSale"; cmd.CommandTimeout = 0; SqlParameter parameter = new SqlParameter("@FFullNumber", SqlDbType.VarChar, 40); parameter.Direction = ParameterDirection.ReturnValue; parameter.Value = GetExcelCon(); cmd.Parameters.Add(parameter); SqlDataAdapter Adapter = new SqlDataAdapter(); Adapter.SelectCommand = cmd; DataSet ds = new DataSet(); Adapter.Fill(ds); return ds.Tables[0]; } }
-
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful.
Visual C++ MVP -
主要是执行存储过程这段,从Excel表中转化成DataTable,从该DataTable表中的某一列提取数据到存储过程去执行,这样的代码该如何写?版主说得很模糊,偶不太明白……
private DataTable SampleData { get { string connection = "Server=192.100.3.3;Datasource=AIS20090330165131;uid=sa;pwd=;"; SqlConnection thisconntion = new SqlConnection(connection); SqlCommand cmd = thisconntion.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "t_StoreAndSale"; cmd.CommandTimeout = 0; SqlParameter parameter = new SqlParameter("@FFullNumber", SqlDbType.VarChar, 40); parameter.Direction = ParameterDirection.ReturnValue; parameter.Value = GetExcelCon(); cmd.Parameters.Add(parameter); SqlDataAdapter Adapter = new SqlDataAdapter(); Adapter.SelectCommand = cmd; DataSet ds = new DataSet(); Adapter.Fill(ds); return ds.Tables[0]; } } -
你好,
参考下面代码试试。
private List<string> GetExcelCon() { string connection = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = C:\\Documents and Settings\\yangjh\\桌面\\sample.xls;Extended Properties=Excel 8.0"; OleDbConnection thisconnection = new OleDbConnection(connection); string command = "select * from [sheet1$]"; thisconnection.Open(); OleDbDataAdapter adapter = new OleDbDataAdapter(command, thisconnection); DataSet ds = new DataSet(); adapter.Fill(ds, "[sheet1$]"); thisconnection.Close(); List<string> parameters = new List<string>(); foreach (DataRow Row in ds.Tables[0].Rows) { parameters.Add(Row[0].ToString()); } return parameters; } private DataTable SampleData(string para) { string connection = "Server=192.100.3.3;Datasource=AIS20090330165131;uid=sa;pwd=;"; SqlConnection thisconntion = new SqlConnection(connection); SqlCommand cmd = thisconntion.CreateCommand(); cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "t_StoreAndSale"; cmd.CommandTimeout = 0; SqlParameter parameter = new SqlParameter("@FFullNumber", SqlDbType.VarChar, 40); parameter.Direction = ParameterDirection.ReturnValue; parameter.Value =para; cmd.Parameters.Add(parameter); SqlDataAdapter Adapter = new SqlDataAdapter(); Adapter.SelectCommand = cmd; DataSet ds = new DataSet(); Adapter.Fill(ds); return ds.Tables[0]; } public DataTable GetAllData() { List<string> parameters = GetExcelCon(); DataTable dt = new DataTable(); foreach (string item in parameters) { DataTable returnDt = SampleData(item); if (returnDt!=null&&returnDt.Rows.Count!=0) { dt.Rows.Add(returnDt.Rows[0].ItemArray); } } return dt; }
Microsoft Online Community Support