DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

Sort Excel Ranges in C#

IronXL makes sorting Excel Columns, Rows and Ranges extremely simple in C# and Vb.Net.

Arranging data in alphabetical or value order makes human analysis of data feasible in Microsoft Excel.

C#:

using IronXL;

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet worksheet = workbook.DefaultWorkSheet;

//Select a Range
Range range = worksheet["A1:D20"];

//This is how we can sort the current range in ascending order (A to Z)
range.SortAscending();


//This is how we can sort the current range in descending order (Z to A)
range.SortDescending();

workbook.SaveAs("SortExcelRange.xlsx");
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL

Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private worksheet As WorkSheet = workbook.DefaultWorkSheet

'Select a Range
Private range As Range = worksheet("A1:D20")

'This is how we can sort the current range in ascending order (A to Z)
range.SortAscending()


'This is how we can sort the current range in descending order (Z to A)
range.SortDescending()

workbook.SaveAs("SortExcelRange.xlsx")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)