这个不一定用For循环吧,我这样可以读取excel内容!你可以看看……这个完整的能将excel导入数据库中,你需要的话我发给你!!
这是部分代码:- …………………………
- #region --------读取文件内容到服务器内存----------
- string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + Server.MapPath("fileupload") + "/" + filename + ";Extended Properties=Excel 8.0";
- OleDbConnection thisconnection = new OleDbConnection(conn);
- thisconnection.Open();
- //这是从EXCEL中读取内容,要保证字段名和excel表中的字段名相同
- string Sql = "select taotiName, Que_type, timu_neirong, OptionA, OptionB, OptionC, OptionD,Que_answer from [Sheet1$]";
- OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
- DataSet ds = new DataSet();
- mycommand.Fill(ds, "[Sheet1$]");
- thisconnection.Close();
- this.GridView1.DataSource = ds;
- this.GridView1.DataBind();
- #endregion
- ……………………………………
复制代码 |