Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, viewers learn to sort Excel ranges using the IronXL library in C#, offering an alternative to Excel's built-in functions. After installing IronXL via the NuGet package manager, the tutorial demonstrates how to import the IronXL namespace and load an existing workbook.
The core part of the tutorial shows how to sort data: column A is sorted in ascending order, while column B is sorted in descending order, using the Sort
method with the ascending
and descending
parameters, respectively. The sorted data is saved into a new Excel file named 'sort Excel range.xlsx'. By running the project, users see how these sorting techniques organize data efficiently, making it more readable and easier to analyze.
The tutorial concludes with an invitation to try IronXL’s free trial and encourages viewers to subscribe for more tutorials. This hands-on approach provides a practical understanding of using IronXL for data manipulation in Excel.
Here's an updated, corrected version of the C# code used in this tutorial:
// Import the necessary namespace for IronXL
using IronXL;
class Program
{
static void Main()
{
// Load the existing workbook from a file
WorkBook workbook = WorkBook.Load("path/to/your/existing_workbook.xlsx");
// Select the first worksheet from the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Sort column A in ascending order
sheet["A:A"].Sort(ascending: true);
// Sort column B in descending order
sheet["B:B"].Sort(ascending: false);
// Save the changes to a new Excel file
workbook.SaveAs("sort Excel range.xlsx");
// Notify the user that the operation was successful
Console.WriteLine("Excel range sorted and saved successfully.");
}
}
// Import the necessary namespace for IronXL
using IronXL;
class Program
{
static void Main()
{
// Load the existing workbook from a file
WorkBook workbook = WorkBook.Load("path/to/your/existing_workbook.xlsx");
// Select the first worksheet from the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Sort column A in ascending order
sheet["A:A"].Sort(ascending: true);
// Sort column B in descending order
sheet["B:B"].Sort(ascending: false);
// Save the changes to a new Excel file
workbook.SaveAs("sort Excel range.xlsx");
// Notify the user that the operation was successful
Console.WriteLine("Excel range sorted and saved successfully.");
}
}
' Import the necessary namespace for IronXL
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load the existing workbook from a file
Dim workbook As WorkBook = WorkBook.Load("path/to/your/existing_workbook.xlsx")
' Select the first worksheet from the workbook
Dim sheet As WorkSheet = workbook.WorkSheets.First()
' Sort column A in ascending order
sheet("A:A").Sort(ascending:= True)
' Sort column B in descending order
sheet("B:B").Sort(ascending:= False)
' Save the changes to a new Excel file
workbook.SaveAs("sort Excel range.xlsx")
' Notify the user that the operation was successful
Console.WriteLine("Excel range sorted and saved successfully.")
End Sub
End Class
Comments in Code:
WorkBook.Load()
method is used to open an existing Excel file.WorkSheets.First()
.Sort()
method is called on the ranges "A:A" and "B:B" to sort columns A and B. The ascending
parameter is passed a boolean value to determine the sort order.Further Reading: How to Sort Cell Range