[转]动态获取Excel表名

    public 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);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    tableName += dt.Rows[i][2].ToString().Trim();
                }
            }
        }
        return tableName;
    }
posted @ 2010-10-12 16:49  沙茶叶  阅读(381)  评论(0编辑  收藏  举报