-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathAveragesExample.cs
More file actions
70 lines (58 loc) · 3.04 KB
/
AveragesExample.cs
File metadata and controls
70 lines (58 loc) · 3.04 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
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.Style;
namespace EPPlusSamples.ConditionalFormatting
{
internal class AveragesExample
{
public static void Run(ExcelPackage package)
{
var sheet = package.Workbook.Worksheets.Add("AverageExamples");
sheet.Cells["A1:B21"].Formula = "ROW()";
// -------------------------------------------------------------------
// Create an Above Average rule
// -------------------------------------------------------------------
var above = sheet.ConditionalFormatting.AddAboveAverage(
new ExcelAddress("A1:B21"));
//Properties allow you to change the formatting of conditional formattings
//Multiple font properties can be changed to alter the apperance of the formatting
above.Style.Font.Bold = true;
above.Style.Font.Color.Color = Color.Red;
above.Style.Font.Strike = true;
// -------------------------------------------------------------------
// Create an Above Or Equal Average rule
// -------------------------------------------------------------------
var aboveOrEqual = sheet.ConditionalFormatting.AddAboveOrEqualAverage(
new ExcelAddress("A1:A21"));
//Other properties like style can change background color
aboveOrEqual.Style.Fill.PatternType = ExcelFillStyle.Solid;
aboveOrEqual.Style.Fill.BackgroundColor.Color = Color.DarkBlue;
// -------------------------------------------------------------------
// Create a Below Average rule
// -------------------------------------------------------------------
var belowAverage = sheet.ConditionalFormatting.AddBelowAverage(
new ExcelAddress("A1:B21"));
belowAverage.Style.Fill.PatternType = ExcelFillStyle.Solid;
belowAverage.Style.Fill.BackgroundColor.Color = Color.DarkRed;
// -------------------------------------------------------------------
// Create a Below Or Equal Average rule
// -------------------------------------------------------------------
var belowOrEqual = sheet.ConditionalFormatting.AddBelowOrEqualAverage(
new ExcelAddress("A1:B21"));
belowOrEqual.Style.Font.Color.Color = Color.White;
belowOrEqual.Style.Fill.PatternType = ExcelFillStyle.Solid;
belowOrEqual.Style.Fill.BackgroundColor.Color = Color.DarkGreen;
//Note that when two properties conflict like belowEqual and aboveEqual on the background color the one with the lowest priority number "wins"
//Test switching them around and watch the A11 cells closely.
belowOrEqual.Priority = 2;
aboveOrEqual.Priority = 1;
sheet.Cells.AutoFitColumns();
}
}
}