For excel 2007:
"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";?
For excel2003:
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+str+";Extended Properties=Excel 8.0";
"HDR=yes;"是说第一行是列名而不是数据。"HDR=No;"正好与前面的相反。
如果你想把列名也读到结果集中(使用“HDR=NO”尽管第一行是列名)并且列中的数据是数值型的,使用“IMEX=1”可必免冲突。
使用"IMEX=1"检索混合数据列是一种安全的方法。试想一下,当Driver检索出数据列中有一种数据类型的excel文件可以正常工作,而另一个excel文件(某列)被检测出两种类
型,这会造成你的程序的冲突。
列子
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties='Excel 12.0 Xml;HDR=YES'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
this.GridView1.DataSource = OleDsExcle;
this.GridView1.DataBind();