Skip to main content

How to: Filter Top or Bottom Ranked Values

  • 2 minutes to read

This example demonstrates how to display the top/bottom ranked values using the "Top 10" filter.

  1. Turn on the filtering functionality for the required range, as described in the How to: Enable Filtering example.
  2. Use the AutoFilterBase.Columns property of the SheetAutoFilter object to get a collection of columns in the filtered range (the AutoFilterColumnCollection object). Each column in the collection is defined by the AutoFilterColumn object which provides basic methods for data filtering. To filter data in a particular column, get access to this column by its index in the AutoFilterColumnCollection collection.
  3. Call the AutoFilterColumn.ApplyTop10Filter and pass the following parameters.

    • A filter operator specified by one of the Top10Type enumeration members.
    • A positive integer that defines the number or percentage value to filter by.

    In this example, the AutoFilterColumn.Top10Type property value is equal to Top10Type.Top10Items, and the AutoFilterColumn.Top10Value is set to 10, so that only the top ten sales values are displayed.

View Example

Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;

// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);

// Apply a filter to the "Sales" column to display the top ten values.
worksheet.AutoFilter.Columns[2].ApplyTop10Filter(Top10Type.Top10Items, 10);