日期:2014-05-17 浏览次数:20736 次
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filepicks[k].Text + ";" + "Extended Properties=Excel 8.0;"; OleDbConnection con1 = new OleDbConnection(strcon); con1.Open(); DataTable sheetable = con1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "Table" }); string sheetname = sheetable.Rows[0]["Table_Name"].ToString();//在这一行报错 MessageBox.Show(sheetname); string select_sql = "select * from[" + sheetname + "];"; OleDbDataAdapter adp1 = new OleDbDataAdapter(select_sql, con1);
??????? public static string GetExcelFirstTableName(string excelFileName)
??????? {
??????????? string tableName = null;
??????????? if (File.Exists(excelFileName))
??????????? {
????????????? using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet."+
??????????????? "OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + excelFileName))
????????????? {
????????????????? conn.Open();
????????????????? DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
????????????????? tableName = dt.Rows[0][2].ToString().Trim();
????????????? }
????????? }
??????????? return tableName;
??????? }