DataTable dt = myConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);string tableName = dt.Rows[0][2].ToString().Trim();
这样获取的表名是排序过的 ,不能保证就是第一个。希望高手解答。
9 回答
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
private void ImportData() { string StrConn; StringBuilder sb = new StringBuilder();
sb.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="); sb.Append(FilePath); sb.Append(";Extended Properties='Excel 8.0;HDR=No; IMEX=1'"); StrConn = sb.ToString(); DataTable tempTable; OleDbConnection ConnExcel = new OleDbConnection(StrConn); try { ConnExcel.Open(); System.Data.DataTable schemaTable = ConnExcel.GetOleDbSchemaTable (System.Data.OleDb.OleDbSchemaGuid.Tables, null); string tableName = schemaTable.Rows[0][2].ToString().Trim(); string strSql = "select * from[" + tableName + "]"; OleDbDataAdapter AdptExcel = new OleDbDataAdapter(strSql, ConnExcel); DataSet ds = new DataSet(); AdptExcel.Fill(ds, "Excel"); ConnExcel.Close(); tempTable = ds.Tables["Excel"]; //return; } catch (Exception e) { MyShowMessageBox.Show(this, "请先解密表格"); return; }
- 9 回答
- 0 关注
- 805 浏览
添加回答
举报
0/150
提交
取消