forked from go2ismail/EPPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample14.cs
444 lines (366 loc) · 20 KB
/
Sample14.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
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
/*******************************************************************************
* You may amend and distribute as you like, but don't remove this header!
*
* All rights reserved.
*
* EPPlus is an Open Source project provided under the
* GNU General Public License (GPL) as published by the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* EPPlus provides server-side generation of Excel 2007 spreadsheets.
* See https://github.com/JanKallman/EPPlus for details.
*
*
*
* The GNU General Public License can be viewed at http://www.opensource.org/licenses/gpl-license.php
* If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
*
* The code for this project may be used and redistributed by any means PROVIDING it is
* not sold for profit without the author's written consent, and providing that this notice
* and the author's name and all copyright notices remain intact.
*
* All code and executables are provided "as is" with no warranty either express or implied.
* The author accepts no liability for any damage or loss of business that this product may cause.
*
*
* Code change notes:
*
* Author Change Date
*******************************************************************************
* Eyal Seagull Added 2012-04-03
*******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using OfficeOpenXml;
using System.Xml;
using System.Drawing;
using OfficeOpenXml.Style;
using OfficeOpenXml.Style.Dxf;
using OfficeOpenXml.DataValidation;
using OfficeOpenXml.ConditionalFormatting;
namespace EPPlusSamples
{
class Sample14
{
/// <summary>
/// Sample 14 - Conditional formatting example
/// </summary>
public static string RunSample14()
{
FileInfo newFile = Utils.GetFileInfo("sample14.xlsx");
using (ExcelPackage package = new ExcelPackage(newFile))
{
// add a new worksheet to the empty workbook
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Conditional Formatting");
// Create 4 columns of samples data
for (int col = 1; col < 10; col++)
{
// Add the headers
worksheet.Cells[1, col].Value = "Sample " + col;
for (int row = 2; row < 21; row++)
{
// Add some items...
worksheet.Cells[row, col].Value = row;
}
}
// -------------------------------------------------------------------
// TwoColorScale Conditional Formatting example
// -------------------------------------------------------------------
ExcelAddress cfAddress1 = new ExcelAddress("A2:A10");
var cfRule1 = worksheet.ConditionalFormatting.AddTwoColorScale(cfAddress1);
// Now, lets change some properties:
cfRule1.LowValue.Type = eExcelConditionalFormattingValueObjectType.Num;
cfRule1.LowValue.Value = 4;
cfRule1.LowValue.Color = Color.FromArgb(0xFF, 0xFF, 0xEB, 0x84);
cfRule1.HighValue.Type = eExcelConditionalFormattingValueObjectType.Formula;
cfRule1.HighValue.Formula = "IF($G$1=\"A</x:&'cfRule>\",1,5)";
cfRule1.StopIfTrue = true;
cfRule1.Style.Font.Bold = true;
// But others you can't (readonly)
// cfRule1.Type = eExcelConditionalFormattingRuleType.ThreeColorScale;
// -------------------------------------------------------------------
// ThreeColorScale Conditional Formatting example
// -------------------------------------------------------------------
ExcelAddress cfAddress2 = new ExcelAddress(2, 2, 10, 2); //="B2:B10"
var cfRule2 = worksheet.ConditionalFormatting.AddThreeColorScale(cfAddress2);
// Changing some properties again
cfRule2.Priority = 1;
cfRule2.MiddleValue.Type = eExcelConditionalFormattingValueObjectType.Percentile;
cfRule2.MiddleValue.Value = 30;
cfRule2.StopIfTrue = true;
// You can access a rule by its Priority
var cfRule2Priority = cfRule2.Priority;
var cfRule2_1 = worksheet.ConditionalFormatting.RulesByPriority(cfRule2Priority);
// And you can even change the rule's Address
cfRule2_1.Address = new ExcelAddress("Z1:Z3");
// -------------------------------------------------------------------
// Adding another ThreeColorScale in a different way (observe that we are
// pointing to the same range as the first rule we entered. Excel allows it to
// happen and group the rules in one <conditionalFormatting> node)
// -------------------------------------------------------------------
var cfRule3 = worksheet.Cells[cfAddress1.Address].ConditionalFormatting.AddThreeColorScale();
cfRule3.LowValue.Color = Color.LemonChiffon;
// -------------------------------------------------------------------
// Change the rules priorities to change their execution order
// -------------------------------------------------------------------
cfRule3.Priority = 1;
cfRule1.Priority = 2;
cfRule2.Priority = 3;
// -------------------------------------------------------------------
// Create an Above Average rule
// -------------------------------------------------------------------
var cfRule5 = worksheet.ConditionalFormatting.AddAboveAverage(
new ExcelAddress("B11:B20"));
cfRule5.Style.Font.Bold = true;
cfRule5.Style.Font.Color.Color = Color.Red;
cfRule5.Style.Font.Strike = true;
// -------------------------------------------------------------------
// Create an Above Or Equal Average rule
// -------------------------------------------------------------------
var cfRule6 = worksheet.ConditionalFormatting.AddAboveOrEqualAverage(
new ExcelAddress("B11:B20"));
// -------------------------------------------------------------------
// Create a Below Average rule
// -------------------------------------------------------------------
var cfRule7 = worksheet.ConditionalFormatting.AddBelowAverage(
new ExcelAddress("B11:B20"));
// -------------------------------------------------------------------
// Create a Below Or Equal Average rule
// -------------------------------------------------------------------
var cfRule8 = worksheet.ConditionalFormatting.AddBelowOrEqualAverage(
new ExcelAddress("B11:B20"));
// -------------------------------------------------------------------
// Create a Above StdDev rule
// -------------------------------------------------------------------
var cfRule9 = worksheet.ConditionalFormatting.AddAboveStdDev(
new ExcelAddress("B11:B20"));
cfRule9.StdDev = 0;
// -------------------------------------------------------------------
// Create a Below StdDev rule
// -------------------------------------------------------------------
var cfRule10 = worksheet.ConditionalFormatting.AddBelowStdDev(
new ExcelAddress("B11:B20"));
cfRule10.StdDev = 2;
// -------------------------------------------------------------------
// Create a Bottom rule
// -------------------------------------------------------------------
var cfRule11 = worksheet.ConditionalFormatting.AddBottom(
new ExcelAddress("B11:B20"));
cfRule11.Rank = 4;
// -------------------------------------------------------------------
// Create a Bottom Percent rule
// -------------------------------------------------------------------
var cfRule12 = worksheet.ConditionalFormatting.AddBottomPercent(
new ExcelAddress("B11:B20"));
cfRule12.Rank = 15;
// -------------------------------------------------------------------
// Create a Top rule
// -------------------------------------------------------------------
var cfRule13 = worksheet.ConditionalFormatting.AddTop(
new ExcelAddress("B11:B20"));
// -------------------------------------------------------------------
// Create a Top Percent rule
// -------------------------------------------------------------------
var cfRule14 = worksheet.ConditionalFormatting.AddTopPercent(
new ExcelAddress("B11:B20"));
cfRule14.Style.Border.Left.Style = ExcelBorderStyle.Thin;
cfRule14.Style.Border.Left.Color.Theme = 3;
cfRule14.Style.Border.Bottom.Style = ExcelBorderStyle.DashDot;
cfRule14.Style.Border.Bottom.Color.Index=8;
cfRule14.Style.Border.Right.Style = ExcelBorderStyle.Thin;
cfRule14.Style.Border.Right.Color.Color=Color.Blue;
cfRule14.Style.Border.Top.Style = ExcelBorderStyle.Hair;
cfRule14.Style.Border.Top.Color.Auto=true;
// -------------------------------------------------------------------
// Create a Last 7 Days rule
// -------------------------------------------------------------------
ExcelAddress timePeriodAddress = new ExcelAddress("D21:G34 C11:C20");
var cfRule15 = worksheet.ConditionalFormatting.AddLast7Days(
timePeriodAddress);
cfRule15.Style.Fill.PatternType = ExcelFillStyle.LightTrellis;
cfRule15.Style.Fill.PatternColor.Color = Color.BurlyWood;
cfRule15.Style.Fill.BackgroundColor.Color = Color.LightCyan;
// -------------------------------------------------------------------
// Create a Last Month rule
// -------------------------------------------------------------------
var cfRule16 = worksheet.ConditionalFormatting.AddLastMonth(
timePeriodAddress);
cfRule16.Style.NumberFormat.Format = "YYYY";
// -------------------------------------------------------------------
// Create a Last Week rule
// -------------------------------------------------------------------
var cfRule17 = worksheet.ConditionalFormatting.AddLastWeek(
timePeriodAddress);
cfRule17.Style.NumberFormat.Format = "YYYY";
// -------------------------------------------------------------------
// Create a Next Month rule
// -------------------------------------------------------------------
var cfRule18 = worksheet.ConditionalFormatting.AddNextMonth(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a Next Week rule
// -------------------------------------------------------------------
var cfRule19 = worksheet.ConditionalFormatting.AddNextWeek(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a This Month rule
// -------------------------------------------------------------------
var cfRule20 = worksheet.ConditionalFormatting.AddThisMonth(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a This Week rule
// -------------------------------------------------------------------
var cfRule21 = worksheet.ConditionalFormatting.AddThisWeek(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a Today rule
// -------------------------------------------------------------------
var cfRule22 = worksheet.ConditionalFormatting.AddToday(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a Tomorrow rule
// -------------------------------------------------------------------
var cfRule23 = worksheet.ConditionalFormatting.AddTomorrow(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a Yesterday rule
// -------------------------------------------------------------------
var cfRule24 = worksheet.ConditionalFormatting.AddYesterday(
timePeriodAddress);
// -------------------------------------------------------------------
// Create a BeginsWith rule
// -------------------------------------------------------------------
ExcelAddress cellIsAddress = new ExcelAddress("E11:E20");
var cfRule25 = worksheet.ConditionalFormatting.AddBeginsWith(
cellIsAddress);
cfRule25.Text = "SearchMe";
// -------------------------------------------------------------------
// Create a Between rule
// -------------------------------------------------------------------
var cfRule26 = worksheet.ConditionalFormatting.AddBetween(
cellIsAddress);
cfRule26.Formula = "IF(E11>5,10,20)";
cfRule26.Formula2 = "IF(E11>5,30,50)";
// -------------------------------------------------------------------
// Create a ContainsBlanks rule
// -------------------------------------------------------------------
var cfRule27 = worksheet.ConditionalFormatting.AddContainsBlanks(
cellIsAddress);
// -------------------------------------------------------------------
// Create a ContainsErrors rule
// -------------------------------------------------------------------
var cfRule28 = worksheet.ConditionalFormatting.AddContainsErrors(
cellIsAddress);
// -------------------------------------------------------------------
// Create a ContainsText rule
// -------------------------------------------------------------------
var cfRule29 = worksheet.ConditionalFormatting.AddContainsText(
cellIsAddress);
cfRule29.Text = "Me";
// -------------------------------------------------------------------
// Create a DuplicateValues rule
// -------------------------------------------------------------------
var cfRule30 = worksheet.ConditionalFormatting.AddDuplicateValues(
cellIsAddress);
// -------------------------------------------------------------------
// Create an EndsWith rule
// -------------------------------------------------------------------
var cfRule31 = worksheet.ConditionalFormatting.AddEndsWith(
cellIsAddress);
cfRule31.Text = "EndText";
// -------------------------------------------------------------------
// Create an Equal rule
// -------------------------------------------------------------------
var cfRule32 = worksheet.ConditionalFormatting.AddEqual(
cellIsAddress);
cfRule32.Formula = "6";
// -------------------------------------------------------------------
// Create an Expression rule
// -------------------------------------------------------------------
var cfRule33 = worksheet.ConditionalFormatting.AddExpression(
cellIsAddress);
cfRule33.Formula = "E11=E12";
// -------------------------------------------------------------------
// Create a GreaterThan rule
// -------------------------------------------------------------------
var cfRule34 = worksheet.ConditionalFormatting.AddGreaterThan(
cellIsAddress);
cfRule34.Formula = "SE(E11<10,10,65)";
// -------------------------------------------------------------------
// Create a GreaterThanOrEqual rule
// -------------------------------------------------------------------
var cfRule35 = worksheet.ConditionalFormatting.AddGreaterThanOrEqual(
cellIsAddress);
cfRule35.Formula = "35";
// -------------------------------------------------------------------
// Create a LessThan rule
// -------------------------------------------------------------------
var cfRule36 = worksheet.ConditionalFormatting.AddLessThan(
cellIsAddress);
cfRule36.Formula = "36";
// -------------------------------------------------------------------
// Create a LessThanOrEqual rule
// -------------------------------------------------------------------
var cfRule37 = worksheet.ConditionalFormatting.AddLessThanOrEqual(
cellIsAddress);
cfRule37.Formula = "37";
// -------------------------------------------------------------------
// Create a NotBetween rule
// -------------------------------------------------------------------
var cfRule38 = worksheet.ConditionalFormatting.AddNotBetween(
cellIsAddress);
cfRule38.Formula = "333";
cfRule38.Formula2 = "999";
// -------------------------------------------------------------------
// Create a NotContainsBlanks rule
// -------------------------------------------------------------------
var cfRule39 = worksheet.ConditionalFormatting.AddNotContainsBlanks(
cellIsAddress);
// -------------------------------------------------------------------
// Create a NotContainsErrors rule
// -------------------------------------------------------------------
var cfRule40 = worksheet.ConditionalFormatting.AddNotContainsErrors(
cellIsAddress);
// -------------------------------------------------------------------
// Create a NotContainsText rule
// -------------------------------------------------------------------
var cfRule41 = worksheet.ConditionalFormatting.AddNotContainsText(
cellIsAddress);
cfRule41.Text = "NotMe";
// -------------------------------------------------------------------
// Create an NotEqual rule
// -------------------------------------------------------------------
var cfRule42 = worksheet.ConditionalFormatting.AddNotEqual(
cellIsAddress);
cfRule42.Formula = "14";
ExcelAddress cfAddress43 = new ExcelAddress("G2:G10");
var cfRule43 = worksheet.ConditionalFormatting.AddThreeIconSet(cfAddress43, eExcelconditionalFormatting3IconsSetType.TrafficLights1);
ExcelAddress cfAddress44 = new ExcelAddress("H2:H10");
var cfRule44 = worksheet.ConditionalFormatting.AddDatabar(cfAddress44, Color.DarkBlue);
// -----------------------------------------------------------
// Removing Conditional Formatting rules
// -----------------------------------------------------------
// Remove one Rule by its object
//worksheet.ConditionalFormatting.Remove(cfRule1);
// Remove one Rule by index
//worksheet.ConditionalFormatting.RemoveAt(1);
// Remove one Rule by its Priority
//worksheet.ConditionalFormatting.RemoveByPriority(2);
// Remove all the Rules
//worksheet.ConditionalFormatting.RemoveAll();
// set some document properties
package.Workbook.Properties.Title = "Conditional Formatting";
package.Workbook.Properties.Author = "Eyal Seagull";
package.Workbook.Properties.Comments = "This sample demonstrates how to add Conditional Formatting to an Excel 2007 worksheet using EPPlus";
// set some custom property values
package.Workbook.Properties.SetCustomPropertyValue("Checked by", "Eyal Seagull");
package.Workbook.Properties.SetCustomPropertyValue("AssemblyName", "EPPlus");
// save our new workbook and we are done!
package.Save();
}
return newFile.FullName;
}
}
}