-
Notifications
You must be signed in to change notification settings - Fork 295
Sorting Tables
EPPlus has a new interface for sorting tables from version 5.7. With this interface you can sort a table on one or multiple columns based on cell values in ascending or descending order. You can also use a custom list to define the sort order. EPPlus will also update the table's SortState in the Office Open Xml, so the configuration of your last sort operation on a worksheet will be visible in your spreadsheet program.
A table (the ExcelTable class) in EPPlus has a Sort function - this function has different signatures but in this example we will use the variant that takes a lambda (Action<TableSortOptions>) as a parameter.
// The Column function takes the zero based column index in the range
table.Sort(x => x.SortBy.Column(0));
// You can also use the _ColumnNamed(string columnName) function to sort on a column
table.Sort(x => x.SortBy.ColumnNamed("Size")
// The Column function takes the zero based column index in the range
table.Sort(x => x.SortBy.Column(0, eSortDirection.Descending));
// The Column function takes the zero based column index in the range
table.Sort(x => x.SortBy.Column(0).ThenSortBy.Column(2).ThenSortBy.ColumnNamed("Price", eSortDirection.Descending));
A custom list can be used when you want to define your own sort order. In this case we use a list for T-shirt sizes with sort order from S to XL.
// The Column function takes the zero based column index in the range
table.Sort(x => x.SortBy.Column(0).UsingCustomList("S", "M", "L", "XL"));
A custom list can be used when you want to define your own sort order. In this case we use a list for T-shirt sizes with sort order from S to XL.
// The Column function takes the zero based column index in the range
table.Sort(x => x.SortBy.Column(0).ThenSortBy.Column(2).UsingCustomList("S", "M", "L", "XL").ThenSortBy.ColumnNamed("Price"));
Here is an example of configure your sort to ignore case. In Excel this is set once for the entire sort, so it works the same with EPPlus for compatibility reasons. The SortState´s CaseSensitive property will be set to true if CompareOptions is either IgnoreCase or OrdinalIgnoreCase.
table.Sort(x =>
{
x.CompareOptions = CompareOptions.IgnoreCase;
x.SortBy.Column(0).UsingCustomList("S", "M", "L").ThenSortBy.Column(1);
});
With the new sorting functionality added in v 5.7 EPPlus also updates the table's SortState. This means that the sort configuration will be visible in Excel. In this example we use the following sort:
table.Sort(x => x.SortBy.ColumnNamed("Size").UsingCustomList("S", "M", "L", "XL")
.ThenSortBy.ColumnNamed("Price", eSortDirection.Descending)
.ThenSortBy.Column(2).UsingCustomList("Blue", "Yellow"));
Screenshot from Excel 2019
If you open the sort configuration in Excel 2019 (right click the table => Sort => Custom sort), the sort set by EPPlus will be set.
EPPlus Software AB - https://epplussoftware.com
- What is new in EPPlus 5+
- Breaking Changes in EPPlus 5
- Breaking Changes in EPPlus 6
- Breaking Changes in EPPlus 7
- Breaking Changes in EPPlus 8
- Addressing a worksheet
- Dimension/Used range
- Copying ranges/sheets
- Insert/Delete
- Filling ranges
- Sorting ranges
- Taking and skipping columns/rows
- Data validation
- Comments
- Freeze and Split Panes
- Header and Footer
- Hyperlinks
- Autofit columns
- Grouping and Ungrouping Rows and Columns
- Formatting and styling
- The ExcelRange.Text property
- Conditional formatting
- Using Themes
- Working with custom named table- or slicer- styles