GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Excel Sorting

Following example shows how to sort values in a specific cell range of a worksheet.

Sorted cells are written to all output file formats. Active SortState is written only to XLSX file format.

Screenshot
Sorting Screenshot

See the full code below, use Run Example to execute.

using System;
using System.Collections.Generic;
using System.Text;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.ConditionalFormatting;
using GemBox.Spreadsheet.PivotTables;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        ExcelFile ef = new ExcelFile();
        ExcelWorksheet ws = ef.Worksheets.Add("Sorting");

        Random rnd = new Random();

        ws.Cells[0, 0].Value = "Sorted numbers";
        ws.Cells.GetSubrangeAbsolute(0, 0, 0, 1).Merged = true;
        for (int i = 1; i < 10; i++)
            ws.Cells[i, 0].SetValue(rnd.Next(1, 100));

        ws.Cells.GetSubrangeAbsolute(1, 0, 10, 0).Sort(false).By(0).Apply();

        ws.Cells[0, 2].Value = "Sorted strings";
        ws.Cells.GetSubrangeAbsolute(0, 2, 0, 3).Merged = true;
        ws.Cells[1, 2].Value = "John";
        ws.Cells[2, 2].Value = "Jennifer";
        ws.Cells[3, 2].Value = "Toby";
        ws.Cells[4, 2].Value = "Chloe";

        ws.Cells.GetSubrangeAbsolute(1, 2, 4, 2).Sort(false).By(0).Apply();

        ws.Cells[0, 4].Value = "Sorted by column E and after that by column F";
        ws.Cells.GetSubrangeAbsolute(0, 4, 0, 8).Merged = true;
        for (int i = 1; i < 10; i++)
        {
            ws.Cells[i, 4].SetValue(rnd.Next(1, 4));
            ws.Cells[i, 5].SetValue(rnd.Next(0, 10));
        }

        // Sort by column E ascending and then by column F descending.
        // These sort settings will be saved to output XLSX file because they are active (parameter value is true).
        ws.Cells.GetSubrangeAbsolute(1, 4, 10, 5).Sort(true).By(0).By(1, true).Apply();

        ef.Save("Sorting.xlsx");
    }
}
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.ConditionalFormatting
Imports GemBox.Spreadsheet.PivotTables

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim ef As ExcelFile = New ExcelFile
        Dim ws As ExcelWorksheet = ef.Worksheets.Add("Sorting")

        Dim rnd = New Random()

        ws.Cells(0, 0).Value = "Sorted numbers"
        For i = 1 To 10 Step 1
            ws.Cells(i, 0).SetValue(rnd.Next(1, 100))
        Next

        ws.Cells.GetSubrangeAbsolute(1, 0, 10, 0).Sort(False).By(0).Apply()

        ws.Cells(0, 2).Value = "Sorted strings"
        ws.Cells(1, 2).Value = "John"
        ws.Cells(2, 2).Value = "Jennifer"
        ws.Cells(3, 2).Value = "Toby"
        ws.Cells(4, 2).Value = "Chloe"

        ws.Cells.GetSubrangeAbsolute(1, 2, 4, 2).Sort(False).By(0).Apply()

        ws.Cells(0, 4).Value = "Sorted by column E and after that by column F"
        For i = 1 To 10 Step 1
            ws.Cells(i, 4).SetValue(rnd.Next(1, 4))
            ws.Cells(i, 5).SetValue(rnd.Next(0, 10))
        Next

        ' Sort by column E ascending and then by column F descending.
        ' These sort settings will be saved to output XLSX file because they are active (parameter value is True).
        ws.Cells.GetSubrangeAbsolute(1, 4, 10, 5).Sort(True).By(0).By(1, True).Apply()

        ef.Save("Sorting.xlsx")

    End Sub

End Module

Check next sample.