ITeye博客

C#获取Excel里sheet名,其表内容

sohighthesky · 2010-06-02 13:22 · C# · 编程语言 · C · C++ · C# · Microsoft · Excel · Markdown

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];
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。