diff --git a/src/LinqToExcel/ExcelQueryFactory.cs b/src/LinqToExcel/ExcelQueryFactory.cs index 6e7755c..8cbf410 100644 --- a/src/LinqToExcel/ExcelQueryFactory.cs +++ b/src/LinqToExcel/ExcelQueryFactory.cs @@ -139,6 +139,17 @@ public IEnumerable GetWorksheetNames() return ExcelUtilities.GetWorksheetNames(FileName); } + /// + /// Returns a list of worksheet names that the spreadsheet contains, in the same order that they would appear in excel + /// + public IEnumerable GetWorksheetNamesOrdered() + { + if (String.IsNullOrEmpty(FileName)) + throw new NullReferenceException("FileName property is not set"); + + return ExcelUtilities.GetWorksheetNamesOrdered(FileName); + } + /// /// Returns a list of columns names that a worksheet contains /// diff --git a/src/LinqToExcel/LinqToExcel.csproj b/src/LinqToExcel/LinqToExcel.csproj index 5309056..228c835 100644 --- a/src/LinqToExcel/LinqToExcel.csproj +++ b/src/LinqToExcel/LinqToExcel.csproj @@ -10,7 +10,7 @@ Properties LinqToExcel LinqToExcel - v3.5 + v4.5 512 @@ -31,6 +31,7 @@ false false true + true @@ -42,6 +43,7 @@ 4 x86 bin\Debug\LinqToExcel.xml + false none @@ -51,6 +53,7 @@ prompt 4 x86 + false true @@ -62,7 +65,7 @@ ..\..\lib\Log4Net\log4net.dll - + False ..\..\lib\Remotion\Remotion.dll @@ -75,6 +78,7 @@ 3.5 + 3.5 diff --git a/src/LinqToExcel/Query/ExcelUtilities.cs b/src/LinqToExcel/Query/ExcelUtilities.cs index 3ab299e..5ef5f45 100644 --- a/src/LinqToExcel/Query/ExcelUtilities.cs +++ b/src/LinqToExcel/Query/ExcelUtilities.cs @@ -7,6 +7,9 @@ using System.IO; using LinqToExcel.Domain; using LinqToExcel.Extensions; +using System.IO.Compression; +using System.Xml; +using System.Xml.Linq; namespace LinqToExcel.Query { @@ -130,6 +133,34 @@ where IsNotBuiltinTable(tableName) return worksheetNames; } + internal static IEnumerable GetWorksheetNamesOrdered(string fileName) + { + if (fileName.ToLower().EndsWith("csv")) + return new List(); + + //open the excel file + using (FileStream data = new FileStream(fileName, FileMode.Open)) + { + //unzip + ZipArchive archive = new ZipArchive(data); + + //select the correct file from the archive + ZipArchiveEntry appxmlFile = archive.Entries.SingleOrDefault(e => e.FullName == "docProps/app.xml"); + + //read the xml + XDocument xdoc = XDocument.Load(appxmlFile.Open()); + + //find the titles element + XElement titlesElement = xdoc.Descendants().Where(e => e.Name.LocalName == "TitlesOfParts").Single(); + + //extract the worksheet names + return titlesElement + .Elements().Where(e => e.Name.LocalName == "vector").Single() + .Elements().Where(e => e.Name.LocalName == "lpstr") + .Select(e => e.Value); + } + } + internal static bool IsTable(DataRow row) { return row["TABLE_NAME"].ToString().Contains("$");