黑马程序员技术交流社区
标题:
.net怎么用for 循环一个不知道多少列的excel
[打印本页]
作者:
刘树斌
时间:
2012-12-30 21:01
标题:
.net怎么用for 循环一个不知道多少列的excel
.net怎么用for 循环一个不知道多少列的excel?
作者:
王晨
时间:
2012-12-30 21:49
这个不一定用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
……………………………………
复制代码
作者:
李立飞
时间:
2013-1-4 08:45
试试foreach()
作者:
杭州-杨
时间:
2013-1-8 11:10
一般我们写的都是不知道多少行,是不是楼主提问写错了,如果是多少行的话,一般从EXCEL取出后,都有个DATATABLE里,然后判断count就可以了,如果是列,一般你获取的时候,就应该写出多少列了,像2楼,已经写好了,多少列了
select taotiName, Que_type, timu_neirong, OptionA, OptionB, OptionC, OptionD,Que_answer from [Sheet1$]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2