Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to get worksheet names in the same order they appear in excel (for review/comments only) #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/LinqToExcel/ExcelQueryFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ public IEnumerable<string> GetWorksheetNames()
return ExcelUtilities.GetWorksheetNames(FileName);
}

/// <summary>
/// Returns a list of worksheet names that the spreadsheet contains, in the same order that they would appear in excel
/// </summary>
public IEnumerable<string> GetWorksheetNamesOrdered()
{
if (String.IsNullOrEmpty(FileName))
throw new NullReferenceException("FileName property is not set");

return ExcelUtilities.GetWorksheetNamesOrdered(FileName);
}

/// <summary>
/// Returns a list of columns names that a worksheet contains
/// </summary>
Expand Down
8 changes: 6 additions & 2 deletions src/LinqToExcel/LinqToExcel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>LinqToExcel</RootNamespace>
<AssemblyName>LinqToExcel</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand All @@ -31,6 +31,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -42,6 +43,7 @@
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<DocumentationFile>bin\Debug\LinqToExcel.xml</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
Expand All @@ -51,6 +53,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
Expand All @@ -62,7 +65,7 @@
<Reference Include="log4net">
<HintPath>..\..\lib\Log4Net\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.DebuggerVisualizers, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.VisualStudio.DebuggerVisualizers, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Remotion, Version=1.13.52.2, Culture=neutral, PublicKeyToken=0669cf0452175907, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\Remotion\Remotion.dll</HintPath>
Expand All @@ -75,6 +78,7 @@
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.IO.Compression" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
Expand Down
31 changes: 31 additions & 0 deletions src/LinqToExcel/Query/ExcelUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -130,6 +133,34 @@ where IsNotBuiltinTable(tableName)
return worksheetNames;
}

internal static IEnumerable<string> GetWorksheetNamesOrdered(string fileName)
{
if (fileName.ToLower().EndsWith("csv"))
return new List<string>();

//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("$");
Expand Down