diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/README.md b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/README.md new file mode 100644 index 00000000..98cc0966 --- /dev/null +++ b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/README.md @@ -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 sortFields = new List(); + + 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(); +} +``` + diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet.sln b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet.sln new file mode 100644 index 00000000..097811f0 --- /dev/null +++ b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet.sln @@ -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 diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Data/Input.xlsx b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Data/Input.xlsx new file mode 100644 index 00000000..7ddc8049 Binary files /dev/null and b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Data/Input.xlsx differ diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Output/.gitkeep b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Program.cs b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Program.cs new file mode 100644 index 00000000..6ed9b4b5 --- /dev/null +++ b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Program.cs @@ -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 sortFields = new List(); + + 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(); + } + } + } +} \ No newline at end of file diff --git a/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Sorting Worksheet.csproj b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Sorting Worksheet.csproj new file mode 100644 index 00000000..c387e3ae --- /dev/null +++ b/Editing Excel cells/Sorting Data with Filters/.NET/Sorting Worksheet/Sorting Worksheet/Sorting Worksheet.csproj @@ -0,0 +1,21 @@ + + + + Exe + net8.0 + Sorting_Worksheet + enable + enable + + + + + + + + + Always + + + +