Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Sorting Data with Filters

Step 1: Create a New C# Console Application Project.

Step 2: Name the Project.

Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org).

Step 4: Include the following namespaces in the **Program.cs** file.

```csharp
using System;
using System.IO;
using Syncfusion.XlsIO;
```

Step 5: Include the below code snippet in **Program.cs** to sort data with filters.
```csharp
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath("Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

//Access sort fields from AutoFilters
ISortFields sortFieldsCollection = worksheet.AutoFilters.DataSorter.SortFields;

//Copy sort fields to a list
List<ISortField> sortFields = new List<ISortField>();

for (int i = 0; i < sortFieldsCollection.Count; i++)
{
sortFields.Add(sortFieldsCollection[i]);
}

//Remove each sort field
foreach (ISortField sortField in sortFields)
{
worksheet.AutoFilters.DataSorter.SortFields.Remove(sortField);
}

//Now re-use the AutoFilters DataSorter
IDataSort sorter = worksheet.AutoFilters.DataSorter;
sorter.SortRange = worksheet.UsedRange;
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
sorter.Sort();

#region Save
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35506.116 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sorting Worksheet", "Sorting Worksheet\Sorting Worksheet.csproj", "{08FD697B-0002-468A-8749-8FE5EFAA808C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.IO;
using Syncfusion.XlsIO;

namespace Sorting_Worksheet
{
class Program
{
static void Main(string[] args)
{
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
IWorkbook workbook = application.Workbooks.Open(inputStream);
IWorksheet worksheet = workbook.Worksheets[0];

//Access sort fields from AutoFilters
ISortFields sortFieldsCollection = worksheet.AutoFilters.DataSorter.SortFields;

//Copy sort fields to a list
List<ISortField> sortFields = new List<ISortField>();

for (int i = 0; i < sortFieldsCollection.Count; i++)
{
sortFields.Add(sortFieldsCollection[i]);
}

//Remove each sort field
foreach (ISortField sortField in sortFields)
{
worksheet.AutoFilters.DataSorter.SortFields.Remove(sortField);
}

//Now re-use the AutoFilters DataSorter
IDataSort sorter = worksheet.AutoFilters.DataSorter;
sorter.SortRange = worksheet.UsedRange;
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
sorter.Sort();

#region Save
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
workbook.SaveAs(outputStream);
#endregion

//Dispose streams
outputStream.Dispose();
inputStream.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Sorting_Worksheet</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>