-
Notifications
You must be signed in to change notification settings - Fork 4
/
AutoFilterActions.vb
321 lines (275 loc) · 15 KB
/
AutoFilterActions.vb
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
Imports DevExpress.Spreadsheet
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms
Namespace SpreadsheetControl_API
Friend Class AutoFilterActions
#Region "Actions"
Public Shared ApplyFilterAction As Action(Of IWorkbook) = AddressOf ApplyFilter
Public Shared FilterAndSortBySingleColumnAction As Action(Of IWorkbook) = AddressOf FilterAndSortBySingleColumn
Public Shared FilterAndSortByMultipleColumnsAction As Action(Of IWorkbook) = AddressOf FilterAndSortByMultipleColumns
Public Shared FilterNumericByConditionAction As Action(Of IWorkbook) = AddressOf FilterNumericByCondition
Public Shared FilterTextByConditionAction As Action(Of IWorkbook) = AddressOf FilterTextByCondition
Public Shared FilterDatesByConditionAction As Action(Of IWorkbook) = AddressOf FilterDatesByCondition
Public Shared FilterByValuesAction As Action(Of IWorkbook) = AddressOf FilterByValue
Public Shared FilterByMultipleValuesAction As Action(Of IWorkbook) = AddressOf FilterByMultipleValues
Public Shared FilterMixedDataTypesByValuesAction As Action(Of IWorkbook) = AddressOf FilterMixedDataTypesByValues
Public Shared Top10FilterAction As Action(Of IWorkbook) = AddressOf Top10FilterValue
Public Shared DynamicFilterAction As Action(Of IWorkbook) = AddressOf DynamicFilterValue
Public Shared ReapplyFilterAction As Action(Of IWorkbook) = AddressOf ReapplyFilterValue
Public Shared ClearFilterAction As Action(Of IWorkbook) = AddressOf ClearFilter
Public Shared DisableFilterAction As Action(Of IWorkbook) = AddressOf DisableFilter
#End Region
Private Shared Sub ApplyFilter(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#ApplyFilter"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' #End Region ' #ApplyFilter
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterAndSortBySingleColumn(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterSortBySingleColumn"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Sort the data in descending order by the first column.
worksheet.AutoFilter.SortState.Sort(0, True)
' #End Region ' #FilterSortBySingleColumn
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterAndSortByMultipleColumns(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterSortByMultipleColumns"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Sort the data in descending order by the first and third columns.
Dim sortConditions As New List(Of SortCondition)()
sortConditions.Add(New SortCondition(0, True))
sortConditions.Add(New SortCondition(2, True))
worksheet.AutoFilter.SortState.Sort(sortConditions)
' #End Region ' #FilterSortByMultipleColumns
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterNumericByCondition(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterByCondition"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Sales" column that are in a range from 5000$ to 8000$.
Dim sales As AutoFilterColumn = worksheet.AutoFilter.Columns(2)
sales.ApplyCustomFilter(5000, FilterComparisonOperator.GreaterThanOrEqual, 8000, FilterComparisonOperator.LessThanOrEqual, True)
' #End Region ' #FilterByCondition
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterTextByCondition(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterTextByCondition"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Filter values in the "Product" column that contain "Gi" and include empty cells.
Dim products As AutoFilterColumn = worksheet.AutoFilter.Columns(1)
products.ApplyCustomFilter("*Gi*", FilterComparisonOperator.Equal, FilterValue.FilterByBlank, FilterComparisonOperator.Equal, False)
' #End Region ' #FilterTextByCondition
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterByValue(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterByValue"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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")
' #End Region ' #FilterByValue
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterByMultipleValues(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterByValues"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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"})
' #End Region ' #FilterByValues
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterDatesByCondition(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterDatesByCondition"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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 Date(2014, 6, 1), FilterComparisonOperator.GreaterThanOrEqual, New Date(2015, 2, 1), FilterComparisonOperator.LessThanOrEqual, True)
' #End Region ' #FilterDatesByCondition
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub FilterMixedDataTypesByValues(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#FilterMixedDataTypesByValues"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Create date grouping item to filter January 2015 dates.
Dim groupings As IList(Of DateGrouping) = New List(Of DateGrouping)()
Dim dateGroupingJan2015 As New DateGrouping(New Date(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)
' #End Region ' #FilterMixedDataTypesByValues
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub Top10FilterValue(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#Top10Filter"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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)
' #End Region ' #Top10Filter
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub DynamicFilterValue(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#DynamicFilter"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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)
' #End Region ' #DynamicFilter
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub ReapplyFilterValue(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#ReapplyFilter"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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()
' #End Region ' #ReapplyFilter
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub ClearFilter(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#ClearFilter"
' Enable filtering for the specified cell range.
Dim range As CellRange = 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()
' #End Region ' #ClearFilter
Finally
workbook.EndUpdate()
End Try
End Sub
Private Shared Sub DisableFilter(ByVal workbook As IWorkbook)
workbook.LoadDocument("Documents\SalesReport.xlsx")
workbook.BeginUpdate()
Try
Dim worksheet As Worksheet = workbook.Worksheets("Regional sales")
workbook.Worksheets.ActiveWorksheet = worksheet
' #Region "#DisableFilter"
' Enable filtering for the specified cell range.
Dim range As CellRange = worksheet("B2:E23")
worksheet.AutoFilter.Apply(range)
' Disable filtering for the entire worksheet.
worksheet.AutoFilter.Disable()
' #End Region ' #DisableFilter
Finally
workbook.EndUpdate()
End Try
End Sub
End Class
End Namespace