-
Notifications
You must be signed in to change notification settings - Fork 1
/
AutoFilterActions.cs
337 lines (292 loc) · 14.4 KB
/
AutoFilterActions.cs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using DevExpress.Spreadsheet;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace SpreadsheetControl_WPF_API_Part02
{
class AutoFilterActions
{
#region Actions
public static Action<IWorkbook> ApplyFilterAction = ApplyFilter;
public static Action<IWorkbook> FilterAndSortBySingleColumnAction = FilterAndSortBySingleColumn;
public static Action<IWorkbook> FilterAndSortByMultipleColumnsAction = FilterAndSortByMultipleColumns;
public static Action<IWorkbook> FilterNumericByConditionAction = FilterNumericByCondition;
public static Action<IWorkbook> FilterTextByConditionAction = FilterTextByCondition;
public static Action<IWorkbook> FilterDatesByConditionAction = FilterDatesByCondition;
public static Action<IWorkbook> FilterByValuesAction = FilterByValue;
public static Action<IWorkbook> FilterByMultipleValuesAction = FilterByMultipleValues;
public static Action<IWorkbook> FilterMixedDataTypesByValuesAction = FilterMixedDataTypesByValues;
public static Action<IWorkbook> Top10FilterAction = Top10FilterValue;
public static Action<IWorkbook> DynamicFilterAction = DynamicFilterValue;
public static Action<IWorkbook> ReapplyFilterAction = ReapplyFilterValue;
public static Action<IWorkbook> ClearFilterAction = ClearFilter;
public static Action<IWorkbook> DisableFilterAction = DisableFilter;
#endregion
static void ApplyFilter(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #ApplyFilter
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
#endregion #ApplyFilter
}
finally { workbook.EndUpdate(); }
}
static void FilterAndSortBySingleColumn(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterSortBySingleColumn
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Sort the data in descending order by the first column.
worksheet.AutoFilter.SortState.Sort(0, true);
#endregion #FilterSortBySingleColumn
}
finally { workbook.EndUpdate(); }
}
static void FilterAndSortByMultipleColumns(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterSortByMultipleColumns
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Sort the data in descending order by the first and third columns.
List<SortCondition> sortConditions = new List<SortCondition>();
sortConditions.Add(new SortCondition(0, true));
sortConditions.Add(new SortCondition(2, true));
worksheet.AutoFilter.SortState.Sort(sortConditions);
#endregion #FilterSortByMultipleColumns
}
finally { workbook.EndUpdate(); }
}
static void FilterNumericByCondition(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterByCondition
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter values in the "Sales" column that are in a range from 5000$ to 8000$.
AutoFilterColumn sales = worksheet.AutoFilter.Columns[2];
sales.ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThanOrEqual, 8000, FilterComparisonOperator.LessThanOrEqual, true);
#endregion #FilterByCondition
}
finally { workbook.EndUpdate(); }
}
static void FilterTextByCondition(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterTextByCondition
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter values in the "Product" column that contain "Gi" and include empty cells.
AutoFilterColumn products = worksheet.AutoFilter.Columns[1];
products.ApplyCustomFilter("*Gi*", FilterComparisonOperator.Equal, FilterValue.FilterByBlank, FilterComparisonOperator.Equal, false);
#endregion #FilterTextByCondition
}
finally { workbook.EndUpdate(); }
}
static void FilterByValue(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterByValue
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter the data in the "Product" column by a specific value.
worksheet.AutoFilter.Columns[1].ApplyFilterCriteria("Mozzarella di Giovanni");
#endregion #FilterByValue
}
finally { workbook.EndUpdate(); }
}
static void FilterByMultipleValues(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterByValues
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter the data in the "Product" column by an array of values.
worksheet.AutoFilter.Columns[1].ApplyFilterCriteria(new CellValue[] { "Mozzarella di Giovanni", "Gorgonzola Telino" });
#endregion #FilterByValues
}
finally { workbook.EndUpdate(); }
}
static void FilterDatesByCondition(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterDatesByCondition
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter values in the "Reported Date" column to display dates that are between June 1, 2014 and February 1, 2015.
worksheet.AutoFilter.Columns[3].ApplyCustomFilter(new DateTime(2014, 6, 1), FilterComparisonOperator.GreaterThanOrEqual, new DateTime(2015, 2, 1), FilterComparisonOperator.LessThanOrEqual, true);
#endregion #FilterDatesByCondition
}
finally { workbook.EndUpdate(); }
}
static void FilterMixedDataTypesByValues(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #FilterMixedDataTypesByValues
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Create date grouping item to filter January 2015 dates.
IList<DateGrouping> groupings = new List<DateGrouping>();
DateGrouping dateGroupingJan2015 = new DateGrouping(new DateTime(2015, 1, 1), DateTimeGroupingType.Month);
groupings.Add(dateGroupingJan2015);
// Filter the data in the "Reported Date" column to display values reported in January 2015.
worksheet.AutoFilter.Columns[3].ApplyFilterCriteria("gennaio 2015", groupings);
#endregion #FilterMixedDataTypesByValues
}
finally { workbook.EndUpdate(); }
}
static void Top10FilterValue(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #Top10Filter
// 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);
#endregion #Top10Filter
}
finally { workbook.EndUpdate(); }
}
static void DynamicFilterValue(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #DynamicFilter
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Apply a dynamic filter to the "Sales" column to display only values that are above the average.
worksheet.AutoFilter.Columns[2].ApplyDynamicFilter(DynamicFilterType.AboveAverage);
// Apply a dynamic filter to the "Reported Date" column to display values reported this year.
worksheet.AutoFilter.Columns[3].ApplyDynamicFilter(DynamicFilterType.ThisYear);
#endregion #DynamicFilter
}
finally { workbook.EndUpdate(); }
}
static void ReapplyFilterValue(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #ReapplyFilter
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter values in the "Sales" column that are greater than 5000$.
worksheet.AutoFilter.Columns[2].ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThan);
// Change the data and reapply the filter.
worksheet["D3"].Value = 5000;
worksheet.AutoFilter.ReApply();
#endregion #ReapplyFilter
}
finally { workbook.EndUpdate(); }
}
static void ClearFilter(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #ClearFilter
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Filter values in the "Sales" column that are greater than 5000$.
worksheet.AutoFilter.Columns[2].ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThan);
// Clear the filter.
worksheet.AutoFilter.Clear();
#endregion #ClearFilter
}
finally { workbook.EndUpdate(); }
}
static void DisableFilter(IWorkbook workbook)
{
workbook.LoadDocument("Documents\\SalesReport.xlsx");
workbook.BeginUpdate();
try
{
Worksheet worksheet = workbook.Worksheets["Regional sales"];
workbook.Worksheets.ActiveWorksheet = worksheet;
#region #DisableFilter
// Enable filtering for the specified cell range.
CellRange range = worksheet["B2:E23"];
worksheet.AutoFilter.Apply(range);
// Disable filtering for the entire worksheet.
worksheet.AutoFilter.Disable();
#endregion #DisableFilter
}
finally { workbook.EndUpdate(); }
}
}
}