public static List<string> GetSheetsNames(string path, ExcelVersion excelversion) { string connectionString; List<string> sheets = new List<string>(); if (excelversion == ExcelVersion.Excel2003) { connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\";"; } else { connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";"; } DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb"); DbConnection connection = factory.CreateConnection(); connection.ConnectionString = connectionString; connection.Open(); DataTable tbl = connection.GetSchema("Tables"); connection.Close(); foreach (DataRow row in tbl.Rows) { string sheetName = (string)row["TABLE_NAME"]; if (sheetName.EndsWith("$")) { sheetName = sheetName.Substring(0, sheetName.Length - 1); } sheets.Add(sheetName); } return sheets; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)