-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSortActions.cs
More file actions
165 lines (135 loc) · 6.62 KB
/
SortActions.cs
File metadata and controls
165 lines (135 loc) · 6.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using DevExpress.Spreadsheet;
using DevExpress.Utils;
using System.Globalization;
using System.Collections.Generic;
namespace SpreadsheetControl_API
{
public static class SortActions {
#region Actions
public static Action<IWorkbook> SimpleSortAction = SimpleSortValue;
public static Action<IWorkbook> DescendingOrderAction = DescendingOrderValue;
public static Action<IWorkbook> SelectComparerAction = SelectComparerValue;
public static Action<IWorkbook> SortBySpecifiedColumnAction = SortBySpecifiedColumnValue;
public static Action<IWorkbook> SortByMultipleColumnsAction = SortByMultipleColumnsValue;
public static Action<IWorkbook> SortByFillColorAction = SortByFillColorValue;
public static Action<IWorkbook> SortByFontColorAction = SortByFontColorValue;
#endregion
static void SimpleSortValue(IWorkbook workbook) {
#region #SimpleSort
Worksheet worksheet = workbook.Worksheets[0];
// Fill in the range.
worksheet.Cells["A2"].Value = "Donald Dozier Bradley";
worksheet.Cells["A3"].Value = "Tony Charles Mccallum-Geteer";
worksheet.Cells["A4"].Value = "Calvin Liu";
worksheet.Cells["A5"].Value = "Anita A Boyd";
worksheet.Cells["A6"].Value = "Angela R. Scott";
worksheet.Cells["A7"].Value = "D Fox";
// Sort the range in ascending order.
CellRange range = worksheet.Range["A2:A7"];
worksheet.Sort(range);
// Create a heading.
CellRange header = worksheet.Range["A1"];
header[0].Value = "Ascending order";
header.ColumnWidthInCharacters = 30;
header.Style = workbook.Styles["Heading 1"];
#endregion #SimpleSort
}
static void DescendingOrderValue(IWorkbook workbook) {
#region #DescendingOrder
Worksheet worksheet = workbook.Worksheets[0];
// Fill in the range.
worksheet.Cells["A2"].Value = "Donald Dozier Bradley";
worksheet.Cells["A3"].Value = "Tony Charles Mccallum-Geteer";
worksheet.Cells["A4"].Value = "Calvin Liu";
worksheet.Cells["A5"].Value = "Anita A Boyd";
worksheet.Cells["A6"].Value = "Angela R. Scott";
worksheet.Cells["A7"].Value = "D Fox";
// Sort the range in descending order.
CellRange range = worksheet.Range["A2:A7"];
worksheet.Sort(range, false);
// Create a heading.
CellRange header = worksheet.Range["A1"];
header[0].Value = "Descending order";
header.ColumnWidthInCharacters = 30;
header.Style = workbook.Styles["Heading 1"];
#endregion #DescendingOrder
}
static void SelectComparerValue(IWorkbook workbook) {
#region #SelectComparer
Worksheet worksheet = workbook.Worksheets[0];
// Fill in the range.
worksheet.Cells["A2"].Value = "Donald Dozier Bradley";
worksheet.Cells["A3"].Value = "Tony Charles Mccallum-Geteer";
worksheet.Cells["A4"].Value = "Calvin Liu";
worksheet.Cells["A5"].Value = "Anita A Boyd";
worksheet.Cells["A6"].Value = "Angela R. Scott";
worksheet.Cells["A7"].Value = "D Fox";
// Sort values using a custom comparer.
CellRange range = worksheet.Range["A2:A7"];
worksheet.Sort(range, 0, new SampleComparer());
// Create a heading.
CellRange header = worksheet.Range["A1"];
header[0].Value = "Use a custom comparer";
header.ColumnWidthInCharacters = 30;
header.Style = workbook.Styles["Heading 1"];
#endregion #SelectComparer
}
static void SortBySpecifiedColumnValue(IWorkbook workbook) {
#region #SortBySpecifiedColumn
workbook.LoadDocument("Documents\\Sortsample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
// Sort by a column with offset = 6 in the range being sorted.
// Use ascending order.
CellRange range = worksheet.Range["A3:F22"];
worksheet.Sort(range, 3);
// Add a note.
worksheet["D1"].Value = "Sort by column with index = 3 in ascending order";
worksheet.Visible = true;
#endregion #SortBySpecifiedColumn
}
static void SortByMultipleColumnsValue(IWorkbook workbook) {
#region #SortByMultipleColumns
workbook.LoadDocument("Documents\\Sortsample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
// Create sorting fields.
List<SortField> fields = new List<SortField>();
// First sorting field. First column (offset = 0) will be sorted using ascending order.
SortField sortField1 = new SortField();
sortField1.ColumnOffset = 0;
sortField1.Comparer = worksheet.Comparers.Ascending;
fields.Add(sortField1);
// Second sorting field. Second column (offset = 1) will be sorted using ascending order.
SortField sortField2 = new SortField();
sortField2.ColumnOffset = 1;
sortField2.Comparer = worksheet.Comparers.Ascending;
fields.Add(sortField2);
// Sort the range by sorting fields.
CellRange range = worksheet.Range["A3:F22"];
worksheet.Sort(range, fields);
#endregion #SortByMultipleColumns
// Add a note.
worksheet["D1"].Value = "Sort by two columns: first and second in ascending order";
}
static void SortByFillColorValue(IWorkbook workbook)
{
#region #SortByFillColor
workbook.LoadDocument("Documents\\Sortsample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
// Sort the "A3:F22" range by column "A" in ascending order.
CellRange range = worksheet.Range["A3:F22"];
worksheet.Sort(range, 0, worksheet["A3"].Fill);
#endregion #SortByFillColor
}
static void SortByFontColorValue(IWorkbook workbook)
{
#region #SortByFontColor
workbook.LoadDocument("Documents\\Sortsample.xlsx");
Worksheet worksheet = workbook.Worksheets[0];
// Sort the "A3:F22" range by column "F" in ascending order.
CellRange range = worksheet.Range["A3:F22"];
worksheet.Sort(range, 5, worksheet["F12"].Font.Color);
#endregion #SortByFontColor
}
}
}