How to Sort Cell Ranges in Excel Using IronXL

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
$vbLabelText   $csharpLabel

Comments in Code:

  • The IronXL namespace is imported to gain access to its functions.
  • The WorkBook.Load() method is used to open an existing Excel file.
  • We access the first worksheet of this workbook using WorkSheets.First().
  • The 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.
  • The sorted workbook is saved as 'sort Excel range.xlsx'.
  • A console message is displayed to inform the user that the sorting process is completed.

Further Reading: How to Sort Cell Range

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Create and Read Excel Files in VB .NET
NEXT >
How to Set a Password on an Excel Worksheet in C#