Skip to end of metadata
Go to start of metadata

Data sorting is one of Microsoft Excel's many useful features. It allows users to order data to make it easier to scan. Aspose.Cells lets developers sort worksheet data alphabetically or numerically which works in the same way as Microsoft Excel does to sort data.

Contents Summary

Sorting Data in Microsoft Excel

To sort data in Microsoft Excel:

  1. Select Data from the Sort menu. The Sort dialog will be displayed.
  2. Select a sorting option.

Generally, sorting is performed on a list - defined as a contiguous group of data where the data is displayed in columns.

Sorting Data with Aspose.Cells

Aspose.Cells provides the DataSorter class used to sort data in ascending or descending order. The class has some important members, for example properties like Key1 ... Key3 and Order1 ... Order3. These members are used to define sorted keys and specify the key sort order.

You have to define keys and set the sort order before implementing data sorting. The class provides the Sort method used to perform data sorting based on the cell data in a worksheet.

The Sort method accepts the following parameters:

  • Aspose.Cells.Cells, the cells for underlying worksheet.
  • Aspose.Cells.CellArea, the range of cells. Define the cell area before applying data sorting.

This example uses the template file "Book1.xls" created in Microsoft Excel. After executing the code below, data is sorted appropriately.


If you want to sort LeftToRight, use the DataSorter.SortLeftToRight attribute.

Sorting data with background colour 

Excel provides feature to sort data based on background colour. Same feature is provided with using Aspose.Cells using DataSorter where SortOnType.CellColor can be used in AddKey() to sort data based on background color. All the cells which contain specified color in the AddKey(), function are placed on top or bottom according to the SortOrder setting and order of the rest of the cells is not changed at all.

Following are the sample files which can be downloaded for testing this feature:



  • No labels