Skip to content

Commit ce1e9fd

Browse files
authored
Merge pull request #175 from SyncfusionExamples/941428-SortingAutofilterExample
941428-UG for apply sorting in the worksheet Autofilter
2 parents 854e6fc + d113d0f commit ce1e9fd

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Sorting Data with Filters
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
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).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.XlsIO;
15+
```
16+
17+
Step 5: Include the below code snippet in **Program.cs** to sort data with filters.
18+
```csharp
19+
using (ExcelEngine excelEngine = new ExcelEngine())
20+
{
21+
IApplication application = excelEngine.Excel;
22+
application.DefaultVersion = ExcelVersion.Xlsx;
23+
FileStream inputStream = new FileStream(Path.GetFullPath("Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
24+
IWorkbook workbook = application.Workbooks.Open(inputStream);
25+
IWorksheet worksheet = workbook.Worksheets[0];
26+
27+
//Access sort fields from AutoFilters
28+
ISortFields sortFieldsCollection = worksheet.AutoFilters.DataSorter.SortFields;
29+
30+
//Copy sort fields to a list
31+
List<ISortField> sortFields = new List<ISortField>();
32+
33+
for (int i = 0; i < sortFieldsCollection.Count; i++)
34+
{
35+
sortFields.Add(sortFieldsCollection[i]);
36+
}
37+
38+
//Remove each sort field
39+
foreach (ISortField sortField in sortFields)
40+
{
41+
worksheet.AutoFilters.DataSorter.SortFields.Remove(sortField);
42+
}
43+
44+
//Now re-use the AutoFilters DataSorter
45+
IDataSort sorter = worksheet.AutoFilters.DataSorter;
46+
sorter.SortRange = worksheet.UsedRange;
47+
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
48+
sorter.Sort();
49+
50+
#region Save
51+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
52+
workbook.SaveAs(outputStream);
53+
#endregion
54+
55+
//Dispose streams
56+
outputStream.Dispose();
57+
inputStream.Dispose();
58+
}
59+
```
60+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sorting Worksheet", "Sorting Worksheet\Sorting Worksheet.csproj", "{08FD697B-0002-468A-8749-8FE5EFAA808C}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{08FD697B-0002-468A-8749-8FE5EFAA808C}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Output/.gitkeep

Whitespace-only changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.XlsIO;
4+
5+
namespace Sorting_Worksheet
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (ExcelEngine excelEngine = new ExcelEngine())
12+
{
13+
IApplication application = excelEngine.Excel;
14+
application.DefaultVersion = ExcelVersion.Xlsx;
15+
FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Input.xlsx"), FileMode.Open, FileAccess.Read);
16+
IWorkbook workbook = application.Workbooks.Open(inputStream);
17+
IWorksheet worksheet = workbook.Worksheets[0];
18+
19+
//Access sort fields from AutoFilters
20+
ISortFields sortFieldsCollection = worksheet.AutoFilters.DataSorter.SortFields;
21+
22+
//Copy sort fields to a list
23+
List<ISortField> sortFields = new List<ISortField>();
24+
25+
for (int i = 0; i < sortFieldsCollection.Count; i++)
26+
{
27+
sortFields.Add(sortFieldsCollection[i]);
28+
}
29+
30+
//Remove each sort field
31+
foreach (ISortField sortField in sortFields)
32+
{
33+
worksheet.AutoFilters.DataSorter.SortFields.Remove(sortField);
34+
}
35+
36+
//Now re-use the AutoFilters DataSorter
37+
IDataSort sorter = worksheet.AutoFilters.DataSorter;
38+
sorter.SortRange = worksheet.UsedRange;
39+
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
40+
sorter.Sort();
41+
42+
#region Save
43+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
44+
workbook.SaveAs(outputStream);
45+
#endregion
46+
47+
//Dispose streams
48+
outputStream.Dispose();
49+
inputStream.Dispose();
50+
}
51+
}
52+
}
53+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Sorting_Worksheet</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>

0 commit comments

Comments
 (0)