string KZ = openFileDialog1.FileName;//通过文件浏览对话框文件绝对路径
string fileExt=Path.GetExtension(KZ);//获取文件名的扩展名
if (File.Exists(KZ))
{
string str = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + KZ +";Extended Properties='Excel 8.0;IMEX=1'";//连接字符串
using (OleDbConnection conn = new OleDbConnection(str)) //创建连接对象
{
conn.Open();//打开连接
DataTable dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);//返回架构信息表
MessageBox.Show(dt.Rows[0][2].ToString().Trim());//返回第一个表名,需要第几个表就是Rows[第几个表][2]
}
}
需要第几个表就是:dt.Rows[第几个表][2]

网友评论