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 the SortAscending
or SortDescending
methods on the selected range or column to apply sorting in the appropriate order. The SortByColumn
method, however, requires two parameters: The first input is the column intended to be sorted and the second input is the sorting order.
Here is an example of how you can sort data using C#:
using IronXL;
class ExcelSorter
{
static void Main(string[] args)
{
// Load the Excel file
WorkBook workbook = WorkBook.Load("example.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets.First();
// To sort the entire sheet in ascending order based on the first column
sheet.SortAscending(sheet["A"]);
// Alternatively, sort a specific range in descending order
sheet.SortDescending(sheet["A2:A10"]);
// Using SortByColumn method to sort by specific column in ascending order
sheet.SortByColumn("B", ExcelSortOrder.Ascending);
// Save the modified workbook
workbook.SaveAs("sorted_example.xlsx");
}
}
using IronXL;
class ExcelSorter
{
static void Main(string[] args)
{
// Load the Excel file
WorkBook workbook = WorkBook.Load("example.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets.First();
// To sort the entire sheet in ascending order based on the first column
sheet.SortAscending(sheet["A"]);
// Alternatively, sort a specific range in descending order
sheet.SortDescending(sheet["A2:A10"]);
// Using SortByColumn method to sort by specific column in ascending order
sheet.SortByColumn("B", ExcelSortOrder.Ascending);
// Save the modified workbook
workbook.SaveAs("sorted_example.xlsx");
}
}
Imports IronXL
Friend Class ExcelSorter
Shared Sub Main(ByVal args() As String)
' Load the Excel file
Dim workbook As WorkBook = WorkBook.Load("example.xlsx")
' Select the first worksheet
Dim sheet As WorkSheet = workbook.WorkSheets.First()
' To sort the entire sheet in ascending order based on the first column
sheet.SortAscending(sheet("A"))
' Alternatively, sort a specific range in descending order
sheet.SortDescending(sheet("A2:A10"))
' Using SortByColumn method to sort by specific column in ascending order
sheet.SortByColumn("B", ExcelSortOrder.Ascending)
' Save the modified workbook
workbook.SaveAs("sorted_example.xlsx")
End Sub
End Class
Explanation:
- Loading the workbook: First, we load an existing Excel file using
WorkBook.Load("example.xlsx")
. - Selecting a worksheet: The line
WorkSheet sheet = workbook.WorkSheets.First()
selects the first worksheet in the Excel file. - Sorting the entire sheet:
sheet.SortAscending(sheet["A"]);
sorts data in the entire sheet based on column A in ascending order. - Sorting a specific range:
sheet.SortDescending(sheet["A2:A10"]);
sorts data within the range A2 to A10 in descending order. - Sorting by column: The
SortByColumn
method sorts data by a specific column, allowing you to specify the sort order (ascending or descending). In this example, we sort column B in ascending order. - Saving the workbook: Lastly, the modified workbook is saved using
workbook.SaveAs("sorted_example.xlsx")
.
For an example in VB.NET, the syntax and comments can be similarly structured to maintain clarity and functionality.