C#如何获取EXCEL所有的sheet表名?

时间:2022-11-08 09:32:10

看以下代码:

在返回的结果里回去 Table_Name列..

 DataTable _Table = GetExcelTableName(@"C:\1.xls");
            for (int i = 0; i != _Table.Rows.Count; i++)
            {
                MessageBox.Show(_Table.Rows[i]["Table_Name"].ToString());
            }

具体方法

/// <summary>  
       /// 获取EXCEL的表 表名字列   
       /// </summary>  
       /// <param name="p_ExcelFile">Excel文件</param>  
       /// <returns>数据表</returns>  
       public static DataTable GetExcelTableName(string p_ExcelFile)  
       {  
           try 
           {  
               if (System.IO.File.Exists(p_ExcelFile))  
               {  
                   OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);  
                   _ExcelConn.Open();  
                   DataTable _Table = _ExcelConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);  
                   _ExcelConn.Close();  
                   return _Table;  
               }  
               return null;  
           }  
           catch 
           {  
               return null;  
           }  
       }