-
Notifications
You must be signed in to change notification settings - Fork 2
/
ConditionalFormattingActions.vb
742 lines (674 loc) · 42.3 KB
/
ConditionalFormattingActions.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
Imports System
Imports System.Drawing
Imports System.Globalization
Imports System.IO
Imports DevExpress.Export.Xl
Imports DevExpress.Spreadsheet
Namespace XLExportExamples
Public NotInheritable Class ConditionalFormattingActions
Private Sub New()
End Sub
#Region "Actions"
Public Shared AverageAction As Action(Of Stream, XlDocumentFormat) = AddressOf Average
Public Shared CellIsAction As Action(Of Stream, XlDocumentFormat) = AddressOf CellIs
Public Shared BlanksAction As Action(Of Stream, XlDocumentFormat) = AddressOf Blanks
Public Shared DuplicatesAction As Action(Of Stream, XlDocumentFormat) = AddressOf Duplicates
Public Shared ExpressionAction As Action(Of Stream, XlDocumentFormat) = AddressOf Expression
Public Shared SpecificTextAction As Action(Of Stream, XlDocumentFormat) = AddressOf SpecificText
Public Shared TimePeriodAction As Action(Of Stream, XlDocumentFormat) = AddressOf TimePeriod
Public Shared Top10Action As Action(Of Stream, XlDocumentFormat) = AddressOf Top10
Public Shared DataBarAction As Action(Of Stream, XlDocumentFormat) = AddressOf DataBar
Public Shared IconSetAction As Action(Of Stream, XlDocumentFormat) = AddressOf IconSet
Public Shared ColorScaleAction As Action(Of Stream, XlDocumentFormat) = AddressOf ColorScale
#End Region
Private Shared Sub Average(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 3
Using cell As IXlCell = row.CreateCell()
cell.Value = i + 1
End Using
Next j
End Using
Next i
' #Region "#AverageRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (A1:A11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10))
' Create the rule highlighting values that are above the average in the cell range.
Dim rule As New XlCondFmtRuleAboveAverage()
rule.Condition = XlCondFmtAverageCondition.Above
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (B1:B11).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10))
' Create the rule highlighting values that are above or equal to the average value in the cell range.
rule = New XlCondFmtRuleAboveAverage()
rule.Condition = XlCondFmtAverageCondition.AboveOrEqual
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (C1:C11).
formatting.Ranges.Add(XlCellRange.FromLTRB(2, 0, 2, 10))
' Create the rule highlighting values that are below the average in the cell range.
rule = New XlCondFmtRuleAboveAverage()
rule.Condition = XlCondFmtAverageCondition.Below
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (D1:D11).
formatting.Ranges.Add(XlCellRange.FromLTRB(3, 0, 3, 10))
' Create the rule highlighting values that are below or equal to the average value in the cell range.
rule = New XlCondFmtRuleAboveAverage()
rule.Condition = XlCondFmtAverageCondition.BelowOrEqual
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #AverageRule
End Using
End Using
End Sub
Private Shared Sub CellIs(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat, New XlFormulaParser())
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
Using cell As IXlCell = row.CreateCell()
cell.Value = i + 1
End Using
Using cell As IXlCell = row.CreateCell()
cell.Value = 12 - i
End Using
End Using
Next i
' #Region "#CellIsRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A1:A11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10))
' Create the rule to highlight cells whose values are less than 5.
Dim rule As New XlCondFmtRuleCellIs()
rule.Operator = XlCondFmtOperator.LessThan
rule.Value = 5
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Create the rule to highlight cells whose values are between 5 and 8.
rule = New XlCondFmtRuleCellIs()
rule.Operator = XlCondFmtOperator.Between
rule.Value = 5
rule.SecondValue = 8
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Neutral
formatting.Rules.Add(rule)
' Create the rule to highlight cells whose values are greater than 8.
rule = New XlCondFmtRuleCellIs()
rule.Operator = XlCondFmtOperator.GreaterThan
rule.Value = 8
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (B1:B11).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10))
' Create the rule to highlight cells whose values are greater than a value calculated by a formula.
rule = New XlCondFmtRuleCellIs()
rule.Operator = XlCondFmtOperator.GreaterThan
rule.Value = "=$A1+3"
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #CellIsRule
End Using
End Using
End Sub
Private Shared Sub Blanks(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 9
Using row As IXlRow = sheet.CreateRow()
Using cell As IXlCell = row.CreateCell()
If (i Mod 2) = 0 Then
cell.Value = i + 1
End If
End Using
End Using
Next i
' #Region "#BlanksRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A1:A10).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 9))
' Create the rule to highlight blank cells in the range.
Dim rule As New XlCondFmtRuleBlanks(True)
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Create the rule to highlight non-blank cells in the range.
rule = New XlCondFmtRuleBlanks(False)
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #BlanksRule
End Using
End Using
End Sub
Private Shared Sub Duplicates(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 3
Using cell As IXlCell = row.CreateCell()
cell.Value = cell.ColumnIndex * cell.RowIndex + cell.RowIndex + 1
End Using
Next j
End Using
Next i
' #Region "#DuplicatesRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A1:D11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 3, 10))
' Create the rule to identify duplicate values in the cell range.
formatting.Rules.Add(New XlCondFmtRuleDuplicates() With {.Formatting = XlCellFormatting.Bad})
' Create the rule to identify unique values in the cell range.
formatting.Rules.Add(New XlCondFmtRuleUnique() With {.Formatting = XlCellFormatting.Good})
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #DuplicatesRule
End Using
End Using
End Sub
Private Shared Sub Expression(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat, New XlFormulaParser())
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
Dim width() As Integer = { 80, 150, 90 }
For i As Integer = 0 To 2
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = width(i)
If i = 2 Then
column.Formatting = New XlCellFormatting()
column.Formatting.NumberFormat = "[$$-409] #,##0.00"
End If
End Using
Next i
Dim columnNames() As String = { "Account ID", "User Name", "Balance" }
Using row As IXlRow = sheet.CreateRow()
Dim headerRowFormatting As New XlCellFormatting()
headerRowFormatting.Font = XlFont.BodyFont()
headerRowFormatting.Font.Bold = True
headerRowFormatting.Border = New XlBorder()
headerRowFormatting.Border.BottomColor = Color.Black
headerRowFormatting.Border.BottomLineStyle = XlBorderLineStyle.Thin
For i As Integer = 0 To 2
Using cell As IXlCell = row.CreateCell()
cell.Value = columnNames(i)
cell.ApplyFormatting(headerRowFormatting)
End Using
Next i
End Using
Dim accountIds() As String = { "A105", "A114", "B013", "C231", "D101", "D105" }
Dim users() As String = { "Berry Dafoe", "Chris Cadwell", "Esta Mangold", "Liam Bell", "Simon Newman", "Wendy Underwood" }
Dim balance() As Integer = { 155, 250, 48, 350, -15, 10 }
For i As Integer = 0 To 5
Using row As IXlRow = sheet.CreateRow()
Using cell As IXlCell = row.CreateCell()
cell.Value = accountIds(i)
End Using
Using cell As IXlCell = row.CreateCell()
cell.Value = users(i)
End Using
Using cell As IXlCell = row.CreateCell()
cell.Value = balance(i)
End Using
End Using
Next i
' #Region "#ExpressionRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A2:C7).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 1, 2, 6))
' Create the rule that uses a formula to highlight cells if a value in the column "C" is greater than 0 and less than 50.
Dim rule As New XlCondFmtRuleExpression("AND($C2>0,$C2<50)")
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlFill.SolidFill(Color.FromArgb(&Hff, &Hff, &Hcc))
formatting.Rules.Add(rule)
' Create the rule that uses a formula to highlight cells if a value in the column "C" is less than or equal to 0.
rule = New XlCondFmtRuleExpression("$C2<=0")
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #ExpressionRule
End Using
End Using
End Sub
Private Shared Sub SpecificText(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
Dim width() As Integer = { 250, 180, 100 }
For i As Integer = 0 To 2
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = width(i)
If i = 2 Then
column.Formatting = New XlCellFormatting()
column.Formatting.NumberFormat = "_([$$-409]* #,##0.00_);_([$$-409]* \(#,##0.00\);_([$$-409]* ""-""??_);_(@_)"
End If
End Using
Next i
Dim columnNames() As String = { "Product", "Delivery", "Sales" }
Using row As IXlRow = sheet.CreateRow()
Dim headerRowFormatting As New XlCellFormatting()
headerRowFormatting.Font = XlFont.BodyFont()
headerRowFormatting.Font.Bold = True
headerRowFormatting.Border = New XlBorder()
headerRowFormatting.Border.BottomColor = Color.Black
headerRowFormatting.Border.BottomLineStyle = XlBorderLineStyle.Thin
For i As Integer = 0 To 2
Using cell As IXlCell = row.CreateCell()
cell.Value = columnNames(i)
cell.ApplyFormatting(headerRowFormatting)
End Using
Next i
End Using
Dim products() As String = { "Camembert Pierrot", "Gorgonzola Telino", "Mascarpone Fabioli", "Mozzarella di Giovanni", "Queso Cabrales", "Raclette Courdavault" }
Dim deliveries() As String = { "USA", "Worldwide", "USA", "Ships worldwide", "Worldwide except EU", "EU" }
Dim sales() As Integer = { 15500, 20250, 12634, 35010, 15234, 10050 }
For i As Integer = 0 To 5
Using row As IXlRow = sheet.CreateRow()
Using cell As IXlCell = row.CreateCell()
cell.Value = products(i)
End Using
Using cell As IXlCell = row.CreateCell()
cell.Value = deliveries(i)
End Using
Using cell As IXlCell = row.CreateCell()
cell.Value = sales(i)
End Using
End Using
Next i
' #Region "#SpecificTextRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (B2:B7).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 1, 1, 6))
' Create the rule to highlight cells that contain the given text.
Dim rule As New XlCondFmtRuleSpecificText(XlCondFmtSpecificTextType.Contains, "worldwide")
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Neutral
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #SpecificTextRule
End Using
End Using
End Sub
Private Shared Sub TimePeriod(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = 100
column.ApplyFormatting(XlNumberFormat.ShortDate)
End Using
For i As Integer = 0 To 9
Using row As IXlRow = sheet.CreateRow()
Using cell As IXlCell = row.CreateCell()
cell.Value = Date.Now.AddDays(row.RowIndex - 5)
End Using
End Using
Next i
' #Region "#TimePeriodRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A1:A10).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 9))
' Create the rule to highlight yesterday's dates in the cell range.
Dim rule As New XlCondFmtRuleTimePeriod()
rule.TimePeriod = XlCondFmtTimePeriod.Yesterday
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Create the rule to highlight today's dates in the cell range.
rule = New XlCondFmtRuleTimePeriod()
rule.TimePeriod = XlCondFmtTimePeriod.Today
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Create the rule to highlight tomorrows's dates in the cell range.
rule = New XlCondFmtRuleTimePeriod()
rule.TimePeriod = XlCondFmtTimePeriod.Tomorrow
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Neutral
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #TimePeriodRule
End Using
End Using
End Sub
Private Shared Sub Top10(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 9
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 3
Using cell As IXlCell = row.CreateCell()
cell.Value = cell.ColumnIndex * 4 + cell.RowIndex + 1
End Using
Next j
End Using
Next i
' #Region "#TopAndBottomRules"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rules should be applied (A1:D10).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 3, 9))
' Create the rule to identify bottom 10 values in the cell range.
Dim rule As New XlCondFmtRuleTop10()
rule.Bottom = True
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Bad
formatting.Rules.Add(rule)
' Create the rule to identify top 10 values in the cell range.
rule = New XlCondFmtRuleTop10()
' Specify formatting settings to be applied to cells if the condition is true.
rule.Formatting = XlCellFormatting.Good
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #TopAndBottomRules
End Using
End Using
End Sub
Private Shared Sub DataBar(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 2
Using column As IXlColumn = sheet.CreateColumn()
column.WidthInPixels = 100
End Using
Next i
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 2
Using cell As IXlCell = row.CreateCell()
Dim rowIndex As Integer = cell.RowIndex
Dim columnIndex As Integer = cell.ColumnIndex
If columnIndex = 0 Then
cell.Value = rowIndex + 1
ElseIf columnIndex = 1 Then
cell.Value = rowIndex - 5
Else
cell.Value = If(rowIndex < 5, rowIndex + 1, 11 - rowIndex)
End If
End Using
Next j
End Using
Next i
' #Region "#DataBarRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (A1:A11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10))
' Create the rule to compare values in the cell range using data bars.
Dim rule As New XlCondFmtRuleDataBar()
' Specify the bar color.
rule.FillColor = XlColor.FromTheme(XlThemeColor.Accent1, 0.2)
' Specify the solid fill type.
rule.GradientFill = False
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (B1:B11).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10))
' Create the rule to compare values in the cell range using data bars.
rule = New XlCondFmtRuleDataBar()
' Set the positive bar color to green.
rule.FillColor = Color.Green
' Set the border color of positive bars to green.
rule.BorderColor = Color.Green
' Set the axis color to brown.
rule.AxisColor = Color.Brown
' Use the gradient fill type
rule.GradientFill = True
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (C1:C11).
formatting.Ranges.Add(XlCellRange.FromLTRB(2, 0, 2, 10))
' Create the rule to compare values in the cell range using data bars.
rule = New XlCondFmtRuleDataBar()
' Specify the bar color.
rule.FillColor = XlColor.FromTheme(XlThemeColor.Accent4, 0.2)
' Set the minimum length of the data bar.
rule.MinLength = 10
' Set the maximum length of the data bar.
rule.MaxLength = 90
' Set the value corresponding to the shortest bar.
rule.MinValue.ObjectType = XlCondFmtValueObjectType.Number
rule.MinValue.Value = 3
' Set the direction of data bars.
rule.Direction = XlDataBarDirection.RightToLeft
' Hide values of cells to which the rule is applied.
rule.ShowValues = False
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #DataBarRule
End Using
End Using
End Sub
Private Shared Sub IconSet(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 3
Using cell As IXlCell = row.CreateCell()
If cell.ColumnIndex Mod 2 = 0 Then
cell.Value = cell.RowIndex + 1
Else
cell.Value = cell.RowIndex - 5
End If
End Using
Next j
End Using
Next i
' #Region "#IconSetRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (A1:A11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10))
' Create the rule to apply a specific icon from the "3 Arrows" icon set to each cell in the range based on its value.
Dim rule As New XlCondFmtRuleIconSet()
rule.IconSetType = XlCondFmtIconSetType.Arrows3
' Set the rule priority.
rule.Priority = 1
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (B1:B11).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10))
' Create the rule to apply a specific icon from the "3 Flags" icon set to each cell in the range based on its value.
rule = New XlCondFmtRuleIconSet()
rule.IconSetType = XlCondFmtIconSetType.Flags3
' Set the rule priority.
rule.Priority = 2
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (C1:C11).
formatting.Ranges.Add(XlCellRange.FromLTRB(2, 0, 2, 10))
' Create the rule to apply a specific icon from the "5 Ratings" icon set to each cell in the range based on its value.
rule = New XlCondFmtRuleIconSet()
rule.IconSetType = XlCondFmtIconSetType.Rating5
' Hide values of cells to which the rule is applied.
rule.ShowValues = False
' Set the rule priority.
rule.Priority = 3
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify the cell range to which the conditional formatting rule should be applied (D1:D11).
formatting.Ranges.Add(XlCellRange.FromLTRB(3, 0, 3, 10))
' Create the rule to apply a specific icon from the "4 Traffic Lights" icon set to each cell in the range based on its value.
rule = New XlCondFmtRuleIconSet()
rule.IconSetType = XlCondFmtIconSetType.TrafficLights4
' Reverse the icon order.
rule.Reverse = True
' Set the rule priority.
rule.Priority = 4
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #IconSetRule
End Using
End Using
End Sub
Private Shared Sub ColorScale(ByVal stream As Stream, ByVal documentFormat As XlDocumentFormat)
' Create an exporter instance.
Dim exporter As IXlExporter = XlExport.CreateExporter(documentFormat)
' Create a new document.
Using document As IXlDocument = exporter.CreateDocument(stream)
document.Options.Culture = CultureInfo.CurrentCulture
' Create a worksheet.
Using sheet As IXlSheet = document.CreateSheet()
' Generate data for the document.
For i As Integer = 0 To 10
Using row As IXlRow = sheet.CreateRow()
For j As Integer = 0 To 3
Using cell As IXlCell = row.CreateCell()
cell.Value = cell.RowIndex + 1
End Using
Next j
End Using
Next i
' #Region "#ColorScaleRule"
' Create an instance of the XlConditionalFormatting class.
Dim formatting As New XlConditionalFormatting()
' Specify cell ranges to which the conditional formatting rule should be applied (A1:A11 and C1:C11).
formatting.Ranges.Add(XlCellRange.FromLTRB(0, 0, 0, 10))
formatting.Ranges.Add(XlCellRange.FromLTRB(2, 0, 2, 10))
' Create the default three-color scale rule to differentiate low, medium and high values in cell ranges.
Dim rule As New XlCondFmtRuleColorScale()
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' Create an instance of the XlConditionalFormatting class.
formatting = New XlConditionalFormatting()
' Specify cell ranges to which the conditional formatting rule should be applied (B1:B11 and D1:D11).
formatting.Ranges.Add(XlCellRange.FromLTRB(1, 0, 1, 10))
formatting.Ranges.Add(XlCellRange.FromLTRB(3, 0, 3, 10))
' Create the two-color scale rule to differentiate low and high values in cell ranges.
rule = New XlCondFmtRuleColorScale()
rule.ColorScaleType = XlCondFmtColorScaleType.ColorScale2
' Set a color corresponding to the minimum value in the cell range.
rule.MinColor = XlColor.FromTheme(XlThemeColor.Light1, 0.0)
' Set a color corresponding to the maximum value in the cell range.
rule.MaxColor = XlColor.FromTheme(XlThemeColor.Accent1, 0.5)
formatting.Rules.Add(rule)
' Add the specified format options to the worksheet collection of conditional formats.
sheet.ConditionalFormattings.Add(formatting)
' #End Region ' #ColorScaleRule
End Using
End Using
End Sub
End Class
End Namespace