您的位置:首页 > 其它

如何获取excel对应的sheet个数和对应的名字

2011-06-02 10:56 585 查看
public static int GetExcelTableCount(string filename)
{
DataTable dt = new DataTable();
OleDbConnection cnnxls = new OleDbConnection();
try
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 8.0;Data Source =" +
filename;
cnnxls.ConnectionString = mystring;
cnnxls.Open();
dt = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
return dt.Rows.Count;
}
finally
{
cnnxls.Close();
}

return 0;
}
public static string[] GetExcelTableNames(string filename)
{
DataTable dt = new DataTable();
OleDbConnection cnnxls = new OleDbConnection();
try
{
string mystring = "Provider = Microsoft.Jet.OLEDB.4.0 ;Extended Properties=Excel 8.0;Data Source =" +
filename;
cnnxls.ConnectionString = mystring;
cnnxls.Open();
dt = cnnxls.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
}
finally
{
cnnxls.Close();
}
List names = new List();
foreach (DataRow row in dt.Rows)
{
names.Add(row["TABLE_NAME"].ToString().Trim('/'', '$').Replace("''", "'").Replace("$$", "$"));
}
return names.ToArray();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: