using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;
// Select a range
var range = workSheet["A1:D20"];
// Select a column(B)
var column = workSheet.GetColumn(1);
// Sort the range in ascending order (A to Z)
range.SortAscending();
// Sort the range by column(C) in ascending order
range.SortByColumn("C", SortOrder.Ascending);
// Sort the column(B) in descending order (Z to A)
column.SortDescending();
workBook.SaveAs("sortExcelRange.xlsx");
Imports IronXL
Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet
' Select a range
Private range = workSheet("A1:D20")
' Select a column(B)
Private column = workSheet.GetColumn(1)
' Sort the range in ascending order (A to Z)
range.SortAscending()
' Sort the range by column(C) in ascending order
range.SortByColumn("C", SortOrder.Ascending)
' Sort the column(B) in descending order (Z to A)
column.SortDescending()
workBook.SaveAs("sortExcelRange.xlsx")
Install-Package IronXL.Excel
Sort Excel Ranges in C#
Arranging data in alphabetical or value order makes human analysis of data feasible in Microsoft Excel. IronXL makes sorting Excel Columns, Rows and Ranges extremely simple in C# and VB.NET.
Use SortAscending or SortDescending method on the selected range or column to apply sorting in appropriate order. SortByColumn method, however required two parameters. The first input is the column intended to be sorted and the second input the sorting order.
Related Docs Links
Ready to get started? Version: 2024.10 just released