-
Notifications
You must be signed in to change notification settings - Fork 306
Sorting ranges
EPPlus has a new interface and extended functionality for sorting ranges from version 5.7. With this interface you can either sort the rows of a range on one or multiple columns (top-down) or sort the columns of a range on one or multiple rows - in both cases sorting is based on cell values. Ascending as well as descending sort direction is supported. You can also use a custom list to define the sort order. EPPlus will also update the SortState in the Office Open Xml, so the configuration of your last sort operation on a worksheet will be visible in your spreadsheet program.
When EPPlus sorts a range hyperlinks, comments, formulas and metadata of the cells will be adjusted to the new cell address in the spreadsheet.
A range in EPPlus has a Sort function - this function has several different signatures but in this example we will use the variant that takes a lambda (Action<RangeSortOptions>) as a parameter.
// The Column function takes the zero based column index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortBy.Column(0));// The Column function takes the zero based column index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortBy.Column(0, eSortDirection.Descending));// The Column function takes the zero based column index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortBy.Column(0).ThenSortBy.Column(2).ThenSortBy.Column(3, 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
worksheet.Cells["A1:D15"].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
worksheet.Cells["A1:D15"].Sort(x => x.SortBy.Column(0).ThenSortBy.Column(2).UsingCustomList("S", "M", "L", "XL").ThenSortBy.Column(3));// The Row function takes the zero based row index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortLeftToRightBy.Row(0));// The Row function takes the zero based row index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortLeftToRightBy.Row(0, eSortDirection.Descending));// The Rowfunction takes the zero based row index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortLeftToRightBy.Row(0).ThenSortBy.Row(2).ThenSortBy.Row(3, 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 Row function takes the zero based row index in the range
worksheet.Cells["A1:D15"].Sort(x => x.SortLeftToRightBy.Row(0).UsingCustomList("S", "M", "L", "XL"));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