1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | * ******************************************************************************
|
---|
29 | * Eyal Seagull Added 2012-04-03
|
---|
30 | *******************************************************************************/
|
---|
31 | using System;
|
---|
32 | using System.Collections.Generic;
|
---|
33 | using System.Linq;
|
---|
34 | using System.Text;
|
---|
35 | using System.Drawing;
|
---|
36 | using System.Xml;
|
---|
37 | using OfficeOpenXml.ConditionalFormatting.Contracts;
|
---|
38 |
|
---|
39 | namespace OfficeOpenXml.ConditionalFormatting
|
---|
40 | {
|
---|
41 | public class ExcelConditionalFormattingThreeIconSet : ExcelConditionalFormattingIconSetBase<eExcelconditionalFormatting3IconsSetType>
|
---|
42 | {
|
---|
43 | internal ExcelConditionalFormattingThreeIconSet(
|
---|
44 | ExcelAddress address,
|
---|
45 | int priority,
|
---|
46 | ExcelWorksheet worksheet,
|
---|
47 | XmlNode itemElementNode,
|
---|
48 | XmlNamespaceManager namespaceManager)
|
---|
49 | : base(
|
---|
50 | eExcelConditionalFormattingRuleType.ThreeIconSet,
|
---|
51 | address,
|
---|
52 | priority,
|
---|
53 | worksheet,
|
---|
54 | itemElementNode,
|
---|
55 | (namespaceManager == null) ? worksheet.NameSpaceManager : namespaceManager)
|
---|
56 | {
|
---|
57 | }
|
---|
58 | }
|
---|
59 | /// <summary>
|
---|
60 | /// ExcelConditionalFormattingThreeIconSet
|
---|
61 | /// </summary>
|
---|
62 | public class ExcelConditionalFormattingIconSetBase<T>
|
---|
63 | : ExcelConditionalFormattingRule,
|
---|
64 | IExcelConditionalFormattingThreeIconSet<T>
|
---|
65 | {
|
---|
66 | /****************************************************************************************/
|
---|
67 |
|
---|
68 | #region Private Properties
|
---|
69 |
|
---|
70 | #endregion Private Properties
|
---|
71 |
|
---|
72 | /****************************************************************************************/
|
---|
73 |
|
---|
74 | #region Constructors
|
---|
75 | /// <summary>
|
---|
76 | ///
|
---|
77 | /// </summary>
|
---|
78 | /// <param name="type"></param>
|
---|
79 | /// <param name="address"></param>
|
---|
80 | /// <param name="priority"></param>
|
---|
81 | /// <param name="worksheet"></param>
|
---|
82 | /// <param name="itemElementNode"></param>
|
---|
83 | /// <param name="namespaceManager"></param>
|
---|
84 | internal ExcelConditionalFormattingIconSetBase(
|
---|
85 | eExcelConditionalFormattingRuleType type,
|
---|
86 | ExcelAddress address,
|
---|
87 | int priority,
|
---|
88 | ExcelWorksheet worksheet,
|
---|
89 | XmlNode itemElementNode,
|
---|
90 | XmlNamespaceManager namespaceManager)
|
---|
91 | : base(
|
---|
92 | type,
|
---|
93 | address,
|
---|
94 | priority,
|
---|
95 | worksheet,
|
---|
96 | itemElementNode,
|
---|
97 | (namespaceManager == null) ? worksheet.NameSpaceManager : namespaceManager)
|
---|
98 | {
|
---|
99 | if (itemElementNode != null && itemElementNode.HasChildNodes)
|
---|
100 | {
|
---|
101 | int pos = 1;
|
---|
102 | foreach (XmlNode node in itemElementNode.SelectNodes("d:iconSet/d:cfvo", NameSpaceManager))
|
---|
103 | {
|
---|
104 | if(pos==1)
|
---|
105 | {
|
---|
106 | Icon1 = new ExcelConditionalFormattingIconDataBarValue(
|
---|
107 | type,
|
---|
108 | address,
|
---|
109 | worksheet,
|
---|
110 | node,
|
---|
111 | namespaceManager);
|
---|
112 | }
|
---|
113 | else if (pos == 2)
|
---|
114 | {
|
---|
115 | Icon2 = new ExcelConditionalFormattingIconDataBarValue(
|
---|
116 | type,
|
---|
117 | address,
|
---|
118 | worksheet,
|
---|
119 | node,
|
---|
120 | namespaceManager);
|
---|
121 | }
|
---|
122 | else if (pos == 3)
|
---|
123 | {
|
---|
124 | Icon3 = new ExcelConditionalFormattingIconDataBarValue(
|
---|
125 | type,
|
---|
126 | address,
|
---|
127 | worksheet,
|
---|
128 | node,
|
---|
129 | namespaceManager);
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | break;
|
---|
134 | }
|
---|
135 | pos++;
|
---|
136 | }
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | var iconSetNode = CreateComplexNode(
|
---|
141 | Node,
|
---|
142 | ExcelConditionalFormattingConstants.Paths.IconSet);
|
---|
143 |
|
---|
144 | //Create the <iconSet> node inside the <cfRule> node
|
---|
145 | double spann;
|
---|
146 | if (type == eExcelConditionalFormattingRuleType.ThreeIconSet)
|
---|
147 | {
|
---|
148 | spann = 3;
|
---|
149 | }
|
---|
150 | else if (type == eExcelConditionalFormattingRuleType.FourIconSet)
|
---|
151 | {
|
---|
152 | spann = 4;
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | spann = 5;
|
---|
157 | }
|
---|
158 |
|
---|
159 | var iconNode1 = iconSetNode.OwnerDocument.CreateElement(ExcelConditionalFormattingConstants.Paths.Cfvo, ExcelPackage.schemaMain);
|
---|
160 | iconSetNode.AppendChild(iconNode1);
|
---|
161 | Icon1 = new ExcelConditionalFormattingIconDataBarValue(eExcelConditionalFormattingValueObjectType.Percent,
|
---|
162 | 0,
|
---|
163 | "",
|
---|
164 | eExcelConditionalFormattingRuleType.ThreeIconSet,
|
---|
165 | address,
|
---|
166 | priority,
|
---|
167 | worksheet,
|
---|
168 | iconNode1,
|
---|
169 | namespaceManager);
|
---|
170 |
|
---|
171 | var iconNode2 = iconSetNode.OwnerDocument.CreateElement(ExcelConditionalFormattingConstants.Paths.Cfvo, ExcelPackage.schemaMain);
|
---|
172 | iconSetNode.AppendChild(iconNode2);
|
---|
173 | Icon2 = new ExcelConditionalFormattingIconDataBarValue(eExcelConditionalFormattingValueObjectType.Percent,
|
---|
174 | Math.Round(100D / spann, 0),
|
---|
175 | "",
|
---|
176 | eExcelConditionalFormattingRuleType.ThreeIconSet,
|
---|
177 | address,
|
---|
178 | priority,
|
---|
179 | worksheet,
|
---|
180 | iconNode2,
|
---|
181 | namespaceManager);
|
---|
182 |
|
---|
183 | var iconNode3 = iconSetNode.OwnerDocument.CreateElement(ExcelConditionalFormattingConstants.Paths.Cfvo, ExcelPackage.schemaMain);
|
---|
184 | iconSetNode.AppendChild(iconNode3);
|
---|
185 | Icon3 = new ExcelConditionalFormattingIconDataBarValue(eExcelConditionalFormattingValueObjectType.Percent,
|
---|
186 | Math.Round(100D * (2D / spann), 0),
|
---|
187 | "",
|
---|
188 | eExcelConditionalFormattingRuleType.ThreeIconSet,
|
---|
189 | address,
|
---|
190 | priority,
|
---|
191 | worksheet,
|
---|
192 | iconNode3,
|
---|
193 | namespaceManager);
|
---|
194 | Type = type;
|
---|
195 | }
|
---|
196 | }
|
---|
197 |
|
---|
198 | /// <summary>
|
---|
199 | ///
|
---|
200 | /// </summary>
|
---|
201 | ///<param name="type"></param>
|
---|
202 | /// <param name="priority"></param>
|
---|
203 | /// <param name="address"></param>
|
---|
204 | /// <param name="worksheet"></param>
|
---|
205 | /// <param name="itemElementNode"></param>
|
---|
206 | internal ExcelConditionalFormattingIconSetBase(
|
---|
207 | eExcelConditionalFormattingRuleType type,
|
---|
208 | ExcelAddress address,
|
---|
209 | int priority,
|
---|
210 | ExcelWorksheet worksheet,
|
---|
211 | XmlNode itemElementNode)
|
---|
212 | : this(
|
---|
213 | type,
|
---|
214 | address,
|
---|
215 | priority,
|
---|
216 | worksheet,
|
---|
217 | itemElementNode,
|
---|
218 | null)
|
---|
219 | {
|
---|
220 | }
|
---|
221 |
|
---|
222 | /// <summary>
|
---|
223 | ///
|
---|
224 | /// </summary>
|
---|
225 | ///<param name="type"></param>
|
---|
226 | /// <param name="priority"></param>
|
---|
227 | /// <param name="address"></param>
|
---|
228 | /// <param name="worksheet"></param>
|
---|
229 | internal ExcelConditionalFormattingIconSetBase(
|
---|
230 | eExcelConditionalFormattingRuleType type,
|
---|
231 | ExcelAddress address,
|
---|
232 | int priority,
|
---|
233 | ExcelWorksheet worksheet)
|
---|
234 | : this(
|
---|
235 | type,
|
---|
236 | address,
|
---|
237 | priority,
|
---|
238 | worksheet,
|
---|
239 | null,
|
---|
240 | null)
|
---|
241 | {
|
---|
242 | }
|
---|
243 | #endregion Constructors
|
---|
244 |
|
---|
245 | /// <summary>
|
---|
246 | /// Settings for icon 1 in the iconset
|
---|
247 | /// </summary>
|
---|
248 | public ExcelConditionalFormattingIconDataBarValue Icon1
|
---|
249 | {
|
---|
250 | get;
|
---|
251 | internal set;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /// <summary>
|
---|
255 | /// Settings for icon 2 in the iconset
|
---|
256 | /// </summary>
|
---|
257 | public ExcelConditionalFormattingIconDataBarValue Icon2
|
---|
258 | {
|
---|
259 | get;
|
---|
260 | internal set;
|
---|
261 | }
|
---|
262 | /// <summary>
|
---|
263 | /// Settings for icon 2 in the iconset
|
---|
264 | /// </summary>
|
---|
265 | public ExcelConditionalFormattingIconDataBarValue Icon3
|
---|
266 | {
|
---|
267 | get;
|
---|
268 | internal set;
|
---|
269 | }
|
---|
270 | private const string _reversePath = "d:iconSet/@reverse";
|
---|
271 | /// <summary>
|
---|
272 | /// Reverse the order of the icons
|
---|
273 | /// </summary>
|
---|
274 | public bool Reverse
|
---|
275 | {
|
---|
276 | get
|
---|
277 | {
|
---|
278 | return GetXmlNodeBool(_reversePath, false);
|
---|
279 | }
|
---|
280 | set
|
---|
281 | {
|
---|
282 | SetXmlNodeBool(_reversePath, value);
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | private const string _showValuePath = "d:iconSet/@showValue";
|
---|
287 | /// <summary>
|
---|
288 | /// If the cell values are visible
|
---|
289 | /// </summary>
|
---|
290 | public bool ShowValue
|
---|
291 | {
|
---|
292 | get
|
---|
293 | {
|
---|
294 | return GetXmlNodeBool(_showValuePath, true);
|
---|
295 | }
|
---|
296 | set
|
---|
297 | {
|
---|
298 | SetXmlNodeBool(_showValuePath, value);
|
---|
299 | }
|
---|
300 | }
|
---|
301 | private const string _iconSetPath = "d:iconSet/@iconSet";
|
---|
302 | /// <summary>
|
---|
303 | /// Type of iconset
|
---|
304 | /// </summary>
|
---|
305 | public T IconSet
|
---|
306 | {
|
---|
307 | get
|
---|
308 | {
|
---|
309 | var v = GetXmlNodeString(_iconSetPath);
|
---|
310 | v = v.Substring(1); //Skip first icon.
|
---|
311 | return (T)Enum.Parse(typeof(T), v, true);
|
---|
312 | }
|
---|
313 | set
|
---|
314 | {
|
---|
315 | SetXmlNodeString(_iconSetPath, GetIconSetString(value));
|
---|
316 | }
|
---|
317 | }
|
---|
318 | private string GetIconSetString(T value)
|
---|
319 | {
|
---|
320 | if (Type == eExcelConditionalFormattingRuleType.FourIconSet)
|
---|
321 | {
|
---|
322 | switch (value.ToString())
|
---|
323 | {
|
---|
324 | case "Arrows":
|
---|
325 | return "4Arrows";
|
---|
326 | case "ArrowsGray":
|
---|
327 | return "4ArrowsGray";
|
---|
328 | case "Rating":
|
---|
329 | return "4Rating";
|
---|
330 | case "RedToBlack":
|
---|
331 | return "4RedToBlack";
|
---|
332 | case "TrafficLights":
|
---|
333 | return "4TrafficLights";
|
---|
334 | default:
|
---|
335 | throw (new ArgumentException("Invalid type"));
|
---|
336 | }
|
---|
337 | }
|
---|
338 | else if (Type == eExcelConditionalFormattingRuleType.FiveIconSet)
|
---|
339 | {
|
---|
340 | switch (value.ToString())
|
---|
341 | {
|
---|
342 | case "Arrows":
|
---|
343 | return "5Arrows";
|
---|
344 | case "ArrowsGray":
|
---|
345 | return "5ArrowsGray";
|
---|
346 | case "Quarters":
|
---|
347 | return "5Quarters";
|
---|
348 | case "Rating":
|
---|
349 | return "5Rating";
|
---|
350 | default:
|
---|
351 | throw (new ArgumentException("Invalid type"));
|
---|
352 | }
|
---|
353 | }
|
---|
354 | else
|
---|
355 | {
|
---|
356 | switch (value.ToString())
|
---|
357 | {
|
---|
358 | case "Arrows":
|
---|
359 | return "3Arrows";
|
---|
360 | case "ArrowsGray":
|
---|
361 | return "3ArrowsGray";
|
---|
362 | case "Flags":
|
---|
363 | return "3Flags";
|
---|
364 | case "Signs":
|
---|
365 | return "3Signs";
|
---|
366 | case "Symbols":
|
---|
367 | return "3Symbols";
|
---|
368 | case "Symbols2":
|
---|
369 | return "3Symbols2";
|
---|
370 | case "TrafficLights1":
|
---|
371 | return "3TrafficLights1";
|
---|
372 | case "TrafficLights2":
|
---|
373 | return "3TrafficLights2";
|
---|
374 | default:
|
---|
375 | throw (new ArgumentException("Invalid type"));
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | }
|
---|
380 | } |
---|