现在的位置: 首页 > 综合 > 正文

[转]动态获取Excel表名

2012年05月13日 ⁄ 综合 ⁄ 共 424字 ⁄ 字号 评论关闭
    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;
    }

抱歉!评论已关闭.