您的位置:首页 > 编程语言 > C#

C#获取Excel里sheet名

2011-10-13 15:42 399 查看
public static DataTable GetExcelTable(string excelFilename)

{

string connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Jet OLEDB:Engine Type=35;Extended Properties=Excel 8.0;Persist Security Info=False",excelFilename);

DataSet ds = new DataSet();

string tableName;

using (System.Data.OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(connectionString))

{

connection.Open();

DataTable table = connection.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);

tableName = table.Rows[0]["Table_Name"].ToString();

string strExcel = "select * from " + "[" + tableName + "]";

OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, connectionString);

adapter.Fill(ds, tableName);

connection.Close();

}

return ds.Tables[tableName];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: