Skip to content

Commit 53504de

Browse files
941138-SortingCellValues
1 parent ae35238 commit 53504de

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

Editing Excel cells/Sort On Cell Values/.NET/Sort On Cell Values/README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,25 @@ using (ExcelEngine excelEngine = new ExcelEngine())
2929
IDataSort sorter = workbook.CreateDataSorter();
3030

3131
//Range to sort
32-
sorter.SortRange = worksheet.Range["A1:A11"];
32+
sorter.SortRange = worksheet.UsedRange;
3333

34-
//Adds the sort field with the column index, sort based on and order by attribute
34+
//Adds a sort field: sort by values in column A in ascending order
3535
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
3636

37+
//Adds a sort field: then by values in column B in descending order
38+
sorter.SortFields.Add(1, SortOn.Values, OrderBy.Descending);
39+
3740
//Sort based on the sort Field attribute
3841
sorter.Sort();
3942

4043
//Creates the data sorter
4144
sorter = workbook.CreateDataSorter();
4245

4346
//Range to sort
44-
sorter.SortRange = worksheet.Range["B1:B11"];
47+
sorter.SortRange = worksheet.UsedRange;
4548

46-
//Adds the sort field with the column index, sort based on and order by attribute
47-
sorter.SortFields.Add(1, SortOn.Values, OrderBy.Descending);
49+
//Adds a sort field: sort by values in column C in descending order
50+
sorter.SortFields.Add(2, SortOn.Values, OrderBy.Descending);
4851

4952
//Sort based on the sort Field attribute
5053
sorter.Sort();

Editing Excel cells/Sort On Cell Values/.NET/Sort On Cell Values/Sort On Cell Values/Program.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ static void Main(string[] args)
2020
IDataSort sorter = workbook.CreateDataSorter();
2121

2222
//Range to sort
23-
sorter.SortRange = worksheet.Range["A1:A11"];
23+
sorter.SortRange = worksheet.UsedRange;
2424

25-
//Adds the sort field with the column index, sort based on and order by attribute
25+
//Adds a sort field: sort by values in column A in ascending order
2626
sorter.SortFields.Add(0, SortOn.Values, OrderBy.Ascending);
2727

28+
//Adds a sort field: then by values in column B in descending order
29+
sorter.SortFields.Add(1, SortOn.Values, OrderBy.Descending);
30+
2831
//Sort based on the sort Field attribute
2932
sorter.Sort();
3033

3134
//Creates the data sorter
3235
sorter = workbook.CreateDataSorter();
3336

3437
//Range to sort
35-
sorter.SortRange = worksheet.Range["B1:B11"];
38+
sorter.SortRange = worksheet.UsedRange;
3639

37-
//Adds the sort field with the column index, sort based on and order by attribute
38-
sorter.SortFields.Add(1, SortOn.Values, OrderBy.Descending);
40+
//Adds a sort field: sort by values in column C in descending order
41+
sorter.SortFields.Add(2, SortOn.Values, OrderBy.Descending);
3942

4043
//Sort based on the sort Field attribute
4144
sorter.Sort();

0 commit comments

Comments
 (0)