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 | * Jan Källman Initial Release 2009-10-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Globalization;
|
---|
35 | using System.Text;
|
---|
36 | using System.Xml;
|
---|
37 | using System.Drawing;
|
---|
38 | namespace OfficeOpenXml.Style.XmlAccess
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// Xml access class xfs records. This is the top level style object.
|
---|
42 | /// </summary>
|
---|
43 | public sealed class ExcelXfs : StyleXmlHelper
|
---|
44 | {
|
---|
45 | ExcelStyles _styles;
|
---|
46 | internal ExcelXfs(XmlNamespaceManager nameSpaceManager, ExcelStyles styles) : base(nameSpaceManager)
|
---|
47 | {
|
---|
48 | _styles = styles;
|
---|
49 | isBuildIn = false;
|
---|
50 | }
|
---|
51 | internal ExcelXfs(XmlNamespaceManager nsm, XmlNode topNode, ExcelStyles styles) :
|
---|
52 | base(nsm, topNode)
|
---|
53 | {
|
---|
54 | _styles = styles;
|
---|
55 | _xfID = GetXmlNodeInt("@xfId");
|
---|
56 | if (_xfID == 0) isBuildIn = true; //Normal taggen
|
---|
57 | _numFmtId = GetXmlNodeInt("@numFmtId");
|
---|
58 | _fontId = GetXmlNodeInt("@fontId");
|
---|
59 | _fillId = GetXmlNodeInt("@fillId");
|
---|
60 | _borderId = GetXmlNodeInt("@borderId");
|
---|
61 | _readingOrder = GetReadingOrder(GetXmlNodeString(readingOrderPath));
|
---|
62 | _indent = GetXmlNodeInt(indentPath);
|
---|
63 | _shrinkToFit = GetXmlNodeString(shrinkToFitPath) == "1" ? true : false;
|
---|
64 | _verticalAlignment = GetVerticalAlign(GetXmlNodeString(verticalAlignPath));
|
---|
65 | _horizontalAlignment = GetHorizontalAlign(GetXmlNodeString(horizontalAlignPath));
|
---|
66 | _wrapText = GetXmlNodeBool(wrapTextPath);
|
---|
67 | _textRotation = GetXmlNodeInt(textRotationPath);
|
---|
68 | _hidden = GetXmlNodeBool(hiddenPath);
|
---|
69 | _locked = GetXmlNodeBool(lockedPath,true);
|
---|
70 | }
|
---|
71 |
|
---|
72 | private ExcelReadingOrder GetReadingOrder(string value)
|
---|
73 | {
|
---|
74 | switch(value)
|
---|
75 | {
|
---|
76 | case "1":
|
---|
77 | return ExcelReadingOrder.LeftToRight;
|
---|
78 | case "2":
|
---|
79 | return ExcelReadingOrder.RightToLeft;
|
---|
80 | default:
|
---|
81 | return ExcelReadingOrder.ContextDependent;
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | private ExcelHorizontalAlignment GetHorizontalAlign(string align)
|
---|
86 | {
|
---|
87 | if (align == "") return ExcelHorizontalAlignment.General;
|
---|
88 | align = align.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + align.Substring(1, align.Length - 1);
|
---|
89 | try
|
---|
90 | {
|
---|
91 | return (ExcelHorizontalAlignment)Enum.Parse(typeof(ExcelHorizontalAlignment), align);
|
---|
92 | }
|
---|
93 | catch
|
---|
94 | {
|
---|
95 | return ExcelHorizontalAlignment.General;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | private ExcelVerticalAlignment GetVerticalAlign(string align)
|
---|
100 | {
|
---|
101 | if (align == "") return ExcelVerticalAlignment.Bottom;
|
---|
102 | align = align.Substring(0, 1).ToUpper(CultureInfo.InvariantCulture) + align.Substring(1, align.Length - 1);
|
---|
103 | try
|
---|
104 | {
|
---|
105 | return (ExcelVerticalAlignment)Enum.Parse(typeof(ExcelVerticalAlignment), align);
|
---|
106 | }
|
---|
107 | catch
|
---|
108 | {
|
---|
109 | return ExcelVerticalAlignment.Bottom;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | internal void Xf_ChangedEvent(object sender, EventArgs e)
|
---|
113 | {
|
---|
114 | //if (_cell != null)
|
---|
115 | //{
|
---|
116 | // if (!Styles.ChangedCells.ContainsKey(_cell.Id))
|
---|
117 | // {
|
---|
118 | // //_cell.Style = "";
|
---|
119 | // _cell.SetNewStyleID(int.MinValue.ToString());
|
---|
120 | // Styles.ChangedCells.Add(_cell.Id, _cell);
|
---|
121 | // }
|
---|
122 | //}
|
---|
123 | }
|
---|
124 | int _xfID;
|
---|
125 | /// <summary>
|
---|
126 | /// Style index
|
---|
127 | /// </summary>
|
---|
128 | public int XfId
|
---|
129 | {
|
---|
130 | get
|
---|
131 | {
|
---|
132 | return _xfID;
|
---|
133 | }
|
---|
134 | set
|
---|
135 | {
|
---|
136 | _xfID = value;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | #region Internal Properties
|
---|
140 | int _numFmtId;
|
---|
141 | internal int NumberFormatId
|
---|
142 | {
|
---|
143 | get
|
---|
144 | {
|
---|
145 | return _numFmtId;
|
---|
146 | }
|
---|
147 | set
|
---|
148 | {
|
---|
149 | _numFmtId = value;
|
---|
150 | ApplyNumberFormat = (value>0);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | int _fontId;
|
---|
154 | internal int FontId
|
---|
155 | {
|
---|
156 | get
|
---|
157 | {
|
---|
158 | return _fontId;
|
---|
159 | }
|
---|
160 | set
|
---|
161 | {
|
---|
162 | _fontId = value;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | int _fillId;
|
---|
166 | internal int FillId
|
---|
167 | {
|
---|
168 | get
|
---|
169 | {
|
---|
170 | return _fillId;
|
---|
171 | }
|
---|
172 | set
|
---|
173 | {
|
---|
174 | _fillId = value;
|
---|
175 | }
|
---|
176 | }
|
---|
177 | int _borderId;
|
---|
178 | internal int BorderId
|
---|
179 | {
|
---|
180 | get
|
---|
181 | {
|
---|
182 | return _borderId;
|
---|
183 | }
|
---|
184 | set
|
---|
185 | {
|
---|
186 | _borderId = value;
|
---|
187 | }
|
---|
188 | }
|
---|
189 | private bool isBuildIn
|
---|
190 | {
|
---|
191 | get;
|
---|
192 | set;
|
---|
193 | }
|
---|
194 | internal bool ApplyNumberFormat
|
---|
195 | {
|
---|
196 | get;
|
---|
197 | set;
|
---|
198 | }
|
---|
199 | internal bool ApplyFont
|
---|
200 | {
|
---|
201 | get;
|
---|
202 | set;
|
---|
203 | }
|
---|
204 | internal bool ApplyFill
|
---|
205 | {
|
---|
206 | get;
|
---|
207 | set;
|
---|
208 | }
|
---|
209 | internal bool ApplyBorder
|
---|
210 | {
|
---|
211 | get;
|
---|
212 | set;
|
---|
213 | }
|
---|
214 | internal bool ApplyAlignment
|
---|
215 | {
|
---|
216 | get;
|
---|
217 | set;
|
---|
218 | }
|
---|
219 | internal bool ApplyProtection
|
---|
220 | {
|
---|
221 | get;
|
---|
222 | set;
|
---|
223 | }
|
---|
224 | #endregion
|
---|
225 | #region Public Properties
|
---|
226 | public ExcelStyles Styles { get; private set; }
|
---|
227 | /// <summary>
|
---|
228 | /// Numberformat properties
|
---|
229 | /// </summary>
|
---|
230 | public ExcelNumberFormatXml Numberformat
|
---|
231 | {
|
---|
232 | get
|
---|
233 | {
|
---|
234 | return _styles.NumberFormats[_numFmtId < 0 ? 0 : _numFmtId];
|
---|
235 | }
|
---|
236 | }
|
---|
237 | /// <summary>
|
---|
238 | /// Font properties
|
---|
239 | /// </summary>
|
---|
240 | public ExcelFontXml Font
|
---|
241 | {
|
---|
242 | get
|
---|
243 | {
|
---|
244 | return _styles.Fonts[_fontId < 0 ? 0 : _fontId];
|
---|
245 | }
|
---|
246 | }
|
---|
247 | /// <summary>
|
---|
248 | /// Fill properties
|
---|
249 | /// </summary>
|
---|
250 | public ExcelFillXml Fill
|
---|
251 | {
|
---|
252 | get
|
---|
253 | {
|
---|
254 | return _styles.Fills[_fillId < 0 ? 0 : _fillId];
|
---|
255 | }
|
---|
256 | }
|
---|
257 | /// <summary>
|
---|
258 | /// Border style properties
|
---|
259 | /// </summary>
|
---|
260 | public ExcelBorderXml Border
|
---|
261 | {
|
---|
262 | get
|
---|
263 | {
|
---|
264 | return _styles.Borders[_borderId < 0 ? 0 : _borderId];
|
---|
265 | }
|
---|
266 | }
|
---|
267 | const string horizontalAlignPath = "d:alignment/@horizontal";
|
---|
268 | ExcelHorizontalAlignment _horizontalAlignment = ExcelHorizontalAlignment.General;
|
---|
269 | /// <summary>
|
---|
270 | /// Horizontal alignment
|
---|
271 | /// </summary>
|
---|
272 | public ExcelHorizontalAlignment HorizontalAlignment
|
---|
273 | {
|
---|
274 | get
|
---|
275 | {
|
---|
276 | return _horizontalAlignment;
|
---|
277 | }
|
---|
278 | set
|
---|
279 | {
|
---|
280 | _horizontalAlignment = value;
|
---|
281 | }
|
---|
282 | }
|
---|
283 | const string verticalAlignPath = "d:alignment/@vertical";
|
---|
284 | ExcelVerticalAlignment _verticalAlignment=ExcelVerticalAlignment.Bottom;
|
---|
285 | /// <summary>
|
---|
286 | /// Vertical alignment
|
---|
287 | /// </summary>
|
---|
288 | public ExcelVerticalAlignment VerticalAlignment
|
---|
289 | {
|
---|
290 | get
|
---|
291 | {
|
---|
292 | return _verticalAlignment;
|
---|
293 | }
|
---|
294 | set
|
---|
295 | {
|
---|
296 | _verticalAlignment = value;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | const string wrapTextPath = "d:alignment/@wrapText";
|
---|
300 | bool _wrapText=false;
|
---|
301 | /// <summary>
|
---|
302 | /// Wraped text
|
---|
303 | /// </summary>
|
---|
304 | public bool WrapText
|
---|
305 | {
|
---|
306 | get
|
---|
307 | {
|
---|
308 | return _wrapText;
|
---|
309 | }
|
---|
310 | set
|
---|
311 | {
|
---|
312 | _wrapText = value;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | string textRotationPath = "d:alignment/@textRotation";
|
---|
316 | int _textRotation = 0;
|
---|
317 | /// <summary>
|
---|
318 | /// Text rotation angle
|
---|
319 | /// </summary>
|
---|
320 | public int TextRotation
|
---|
321 | {
|
---|
322 | get
|
---|
323 | {
|
---|
324 | return (_textRotation == int.MinValue ? 0 : _textRotation);
|
---|
325 | }
|
---|
326 | set
|
---|
327 | {
|
---|
328 | _textRotation = value;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | const string lockedPath = "d:protection/@locked";
|
---|
332 | bool _locked = true;
|
---|
333 | /// <summary>
|
---|
334 | /// Locked when sheet is protected
|
---|
335 | /// </summary>
|
---|
336 | public bool Locked
|
---|
337 | {
|
---|
338 | get
|
---|
339 | {
|
---|
340 | return _locked;
|
---|
341 | }
|
---|
342 | set
|
---|
343 | {
|
---|
344 | _locked = value;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | const string hiddenPath = "d:protection/@hidden";
|
---|
348 | bool _hidden = false;
|
---|
349 | /// <summary>
|
---|
350 | /// Hide formulas when sheet is protected
|
---|
351 | /// </summary>
|
---|
352 | public bool Hidden
|
---|
353 | {
|
---|
354 | get
|
---|
355 | {
|
---|
356 | return _hidden;
|
---|
357 | }
|
---|
358 | set
|
---|
359 | {
|
---|
360 | _hidden = value;
|
---|
361 | }
|
---|
362 | }
|
---|
363 | const string readingOrderPath = "d:alignment/@readingOrder";
|
---|
364 | ExcelReadingOrder _readingOrder = ExcelReadingOrder.ContextDependent;
|
---|
365 | /// <summary>
|
---|
366 | /// Readingorder
|
---|
367 | /// </summary>
|
---|
368 | public ExcelReadingOrder ReadingOrder
|
---|
369 | {
|
---|
370 | get
|
---|
371 | {
|
---|
372 | return _readingOrder;
|
---|
373 | }
|
---|
374 | set
|
---|
375 | {
|
---|
376 | _readingOrder = value;
|
---|
377 | }
|
---|
378 | }
|
---|
379 | const string shrinkToFitPath = "d:alignment/@shrinkToFit";
|
---|
380 | bool _shrinkToFit = false;
|
---|
381 | /// <summary>
|
---|
382 | /// Shrink to fit
|
---|
383 | /// </summary>
|
---|
384 | public bool ShrinkToFit
|
---|
385 | {
|
---|
386 | get
|
---|
387 | {
|
---|
388 | return _shrinkToFit;
|
---|
389 | }
|
---|
390 | set
|
---|
391 | {
|
---|
392 | _shrinkToFit = value;
|
---|
393 | }
|
---|
394 | }
|
---|
395 | const string indentPath = "d:alignment/@indent";
|
---|
396 | int _indent = 0;
|
---|
397 | /// <summary>
|
---|
398 | /// Indentation
|
---|
399 | /// </summary>
|
---|
400 | public int Indent
|
---|
401 | {
|
---|
402 | get
|
---|
403 | {
|
---|
404 | return (_indent == int.MinValue ? 0 : _indent);
|
---|
405 | }
|
---|
406 | set
|
---|
407 | {
|
---|
408 | _indent=value;
|
---|
409 | }
|
---|
410 | }
|
---|
411 | #endregion
|
---|
412 | internal void RegisterEvent(ExcelXfs xf)
|
---|
413 | {
|
---|
414 | // RegisterEvent(xf, xf.Xf_ChangedEvent);
|
---|
415 | }
|
---|
416 | internal override string Id
|
---|
417 | {
|
---|
418 |
|
---|
419 | get
|
---|
420 | {
|
---|
421 | return XfId + "|" + NumberFormatId.ToString() + "|" + FontId.ToString() + "|" + FillId.ToString() + "|" + BorderId.ToString() + VerticalAlignment.ToString() + "|" + HorizontalAlignment.ToString() + "|" + WrapText.ToString() + "|" + ReadingOrder.ToString() + "|" + isBuildIn.ToString() + TextRotation.ToString() + Locked.ToString() + Hidden.ToString() + ShrinkToFit.ToString() + Indent.ToString();
|
---|
422 | //return Numberformat.Id + "|" + Font.Id + "|" + Fill.Id + "|" + Border.Id + VerticalAlignment.ToString() + "|" + HorizontalAlignment.ToString() + "|" + WrapText.ToString() + "|" + ReadingOrder.ToString();
|
---|
423 | }
|
---|
424 | }
|
---|
425 | internal ExcelXfs Copy()
|
---|
426 | {
|
---|
427 | return Copy(_styles);
|
---|
428 | }
|
---|
429 | internal ExcelXfs Copy(ExcelStyles styles)
|
---|
430 | {
|
---|
431 | ExcelXfs newXF = new ExcelXfs(NameSpaceManager, styles);
|
---|
432 | newXF.NumberFormatId = _numFmtId;
|
---|
433 | newXF.FontId = _fontId;
|
---|
434 | newXF.FillId = _fillId;
|
---|
435 | newXF.BorderId = _borderId;
|
---|
436 | newXF.XfId = _xfID;
|
---|
437 | newXF.ReadingOrder = _readingOrder;
|
---|
438 | newXF.HorizontalAlignment = _horizontalAlignment;
|
---|
439 | newXF.VerticalAlignment = _verticalAlignment;
|
---|
440 | newXF.WrapText = _wrapText;
|
---|
441 | newXF.ShrinkToFit = _shrinkToFit;
|
---|
442 | newXF.Indent = _indent;
|
---|
443 | newXF.TextRotation = _textRotation;
|
---|
444 | newXF.Locked = _locked;
|
---|
445 | newXF.Hidden = _hidden;
|
---|
446 | return newXF;
|
---|
447 | }
|
---|
448 |
|
---|
449 | internal int GetNewID(ExcelStyleCollection<ExcelXfs> xfsCol, StyleBase styleObject, eStyleClass styleClass, eStyleProperty styleProperty, object value)
|
---|
450 | {
|
---|
451 | ExcelXfs newXfs = this.Copy();
|
---|
452 | switch(styleClass)
|
---|
453 | {
|
---|
454 | case eStyleClass.Numberformat:
|
---|
455 | newXfs.NumberFormatId = GetIdNumberFormat(styleProperty, value);
|
---|
456 | styleObject.SetIndex(newXfs.NumberFormatId);
|
---|
457 | break;
|
---|
458 | case eStyleClass.Font:
|
---|
459 | {
|
---|
460 | newXfs.FontId = GetIdFont(styleProperty, value);
|
---|
461 | styleObject.SetIndex(newXfs.FontId);
|
---|
462 | break;
|
---|
463 | }
|
---|
464 | case eStyleClass.Fill:
|
---|
465 | case eStyleClass.FillBackgroundColor:
|
---|
466 | case eStyleClass.FillPatternColor:
|
---|
467 | newXfs.FillId = GetIdFill(styleClass, styleProperty, value);
|
---|
468 | styleObject.SetIndex(newXfs.FillId);
|
---|
469 | break;
|
---|
470 | case eStyleClass.GradientFill:
|
---|
471 | case eStyleClass.FillGradientColor1:
|
---|
472 | case eStyleClass.FillGradientColor2:
|
---|
473 | newXfs.FillId = GetIdGradientFill(styleClass, styleProperty, value);
|
---|
474 | styleObject.SetIndex(newXfs.FillId);
|
---|
475 | break;
|
---|
476 | case eStyleClass.Border:
|
---|
477 | case eStyleClass.BorderBottom:
|
---|
478 | case eStyleClass.BorderDiagonal:
|
---|
479 | case eStyleClass.BorderLeft:
|
---|
480 | case eStyleClass.BorderRight:
|
---|
481 | case eStyleClass.BorderTop:
|
---|
482 | newXfs.BorderId = GetIdBorder(styleClass, styleProperty, value);
|
---|
483 | styleObject.SetIndex(newXfs.BorderId);
|
---|
484 | break;
|
---|
485 | case eStyleClass.Style:
|
---|
486 | switch(styleProperty)
|
---|
487 | {
|
---|
488 | case eStyleProperty.XfId:
|
---|
489 | newXfs.XfId = (int)value;
|
---|
490 | break;
|
---|
491 | case eStyleProperty.HorizontalAlign:
|
---|
492 | newXfs.HorizontalAlignment=(ExcelHorizontalAlignment)value;
|
---|
493 | break;
|
---|
494 | case eStyleProperty.VerticalAlign:
|
---|
495 | newXfs.VerticalAlignment = (ExcelVerticalAlignment)value;
|
---|
496 | break;
|
---|
497 | case eStyleProperty.WrapText:
|
---|
498 | newXfs.WrapText = (bool)value;
|
---|
499 | break;
|
---|
500 | case eStyleProperty.ReadingOrder:
|
---|
501 | newXfs.ReadingOrder = (ExcelReadingOrder)value;
|
---|
502 | break;
|
---|
503 | case eStyleProperty.ShrinkToFit:
|
---|
504 | newXfs.ShrinkToFit=(bool)value;
|
---|
505 | break;
|
---|
506 | case eStyleProperty.Indent:
|
---|
507 | newXfs.Indent = (int)value;
|
---|
508 | break;
|
---|
509 | case eStyleProperty.TextRotation:
|
---|
510 | newXfs.TextRotation = (int)value;
|
---|
511 | break;
|
---|
512 | case eStyleProperty.Locked:
|
---|
513 | newXfs.Locked = (bool)value;
|
---|
514 | break;
|
---|
515 | case eStyleProperty.Hidden:
|
---|
516 | newXfs.Hidden = (bool)value;
|
---|
517 | break;
|
---|
518 | default:
|
---|
519 | throw (new Exception("Invalid property for class style."));
|
---|
520 |
|
---|
521 | }
|
---|
522 | break;
|
---|
523 | default:
|
---|
524 | break;
|
---|
525 | }
|
---|
526 | int id = xfsCol.FindIndexByID(newXfs.Id);
|
---|
527 | if (id < 0)
|
---|
528 | {
|
---|
529 | return xfsCol.Add(newXfs.Id, newXfs);
|
---|
530 | }
|
---|
531 | return id;
|
---|
532 | }
|
---|
533 |
|
---|
534 | private int GetIdBorder(eStyleClass styleClass, eStyleProperty styleProperty, object value)
|
---|
535 | {
|
---|
536 | ExcelBorderXml border = Border.Copy();
|
---|
537 |
|
---|
538 | switch (styleClass)
|
---|
539 | {
|
---|
540 | case eStyleClass.BorderBottom:
|
---|
541 | SetBorderItem(border.Bottom, styleProperty, value);
|
---|
542 | break;
|
---|
543 | case eStyleClass.BorderDiagonal:
|
---|
544 | SetBorderItem(border.Diagonal, styleProperty, value);
|
---|
545 | break;
|
---|
546 | case eStyleClass.BorderLeft:
|
---|
547 | SetBorderItem(border.Left, styleProperty, value);
|
---|
548 | break;
|
---|
549 | case eStyleClass.BorderRight:
|
---|
550 | SetBorderItem(border.Right, styleProperty, value);
|
---|
551 | break;
|
---|
552 | case eStyleClass.BorderTop:
|
---|
553 | SetBorderItem(border.Top, styleProperty, value);
|
---|
554 | break;
|
---|
555 | case eStyleClass.Border:
|
---|
556 | if (styleProperty == eStyleProperty.BorderDiagonalUp)
|
---|
557 | {
|
---|
558 | border.DiagonalUp = (bool)value;
|
---|
559 | }
|
---|
560 | else if (styleProperty == eStyleProperty.BorderDiagonalDown)
|
---|
561 | {
|
---|
562 | border.DiagonalDown = (bool)value;
|
---|
563 | }
|
---|
564 | else
|
---|
565 | {
|
---|
566 | throw (new Exception("Invalid property for class Border."));
|
---|
567 | }
|
---|
568 | break;
|
---|
569 | default:
|
---|
570 | throw (new Exception("Invalid class/property for class Border."));
|
---|
571 | }
|
---|
572 | int subId;
|
---|
573 | string id = border.Id;
|
---|
574 | subId = _styles.Borders.FindIndexByID(id);
|
---|
575 | if (subId == int.MinValue)
|
---|
576 | {
|
---|
577 | return _styles.Borders.Add(id, border);
|
---|
578 | }
|
---|
579 | return subId;
|
---|
580 | }
|
---|
581 |
|
---|
582 | private void SetBorderItem(ExcelBorderItemXml excelBorderItem, eStyleProperty styleProperty, object value)
|
---|
583 | {
|
---|
584 | if(styleProperty==eStyleProperty.Style)
|
---|
585 | {
|
---|
586 | excelBorderItem.Style = (ExcelBorderStyle)value;
|
---|
587 | }
|
---|
588 | else if (styleProperty == eStyleProperty.Color || styleProperty== eStyleProperty.Tint || styleProperty==eStyleProperty.IndexedColor)
|
---|
589 | {
|
---|
590 | if (excelBorderItem.Style == ExcelBorderStyle.None)
|
---|
591 | {
|
---|
592 | throw(new Exception("Can't set bordercolor when style is not set."));
|
---|
593 | }
|
---|
594 | excelBorderItem.Color.Rgb = value.ToString();
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | private int GetIdFill(eStyleClass styleClass, eStyleProperty styleProperty, object value)
|
---|
599 | {
|
---|
600 | ExcelFillXml fill = Fill.Copy();
|
---|
601 |
|
---|
602 | switch (styleProperty)
|
---|
603 | {
|
---|
604 | case eStyleProperty.PatternType:
|
---|
605 | if (fill is ExcelGradientFillXml)
|
---|
606 | {
|
---|
607 | fill = new ExcelFillXml(NameSpaceManager);
|
---|
608 | }
|
---|
609 | fill.PatternType = (ExcelFillStyle)value;
|
---|
610 | break;
|
---|
611 | case eStyleProperty.Color:
|
---|
612 | case eStyleProperty.Tint:
|
---|
613 | case eStyleProperty.IndexedColor:
|
---|
614 | case eStyleProperty.AutoColor:
|
---|
615 | if (fill is ExcelGradientFillXml)
|
---|
616 | {
|
---|
617 | fill = new ExcelFillXml(NameSpaceManager);
|
---|
618 | }
|
---|
619 | if (fill.PatternType == ExcelFillStyle.None)
|
---|
620 | {
|
---|
621 | throw (new ArgumentException("Can't set color when patterntype is not set."));
|
---|
622 | }
|
---|
623 | ExcelColorXml destColor;
|
---|
624 | if (styleClass==eStyleClass.FillPatternColor)
|
---|
625 | {
|
---|
626 | destColor = fill.PatternColor;
|
---|
627 | }
|
---|
628 | else
|
---|
629 | {
|
---|
630 | destColor = fill.BackgroundColor;
|
---|
631 | }
|
---|
632 |
|
---|
633 | if (styleProperty == eStyleProperty.Color)
|
---|
634 | {
|
---|
635 | destColor.Rgb = value.ToString();
|
---|
636 | }
|
---|
637 | else if (styleProperty == eStyleProperty.Tint)
|
---|
638 | {
|
---|
639 | destColor.Tint = (decimal)value;
|
---|
640 | }
|
---|
641 | else if (styleProperty == eStyleProperty.IndexedColor)
|
---|
642 | {
|
---|
643 | destColor.Indexed = (int)value;
|
---|
644 | }
|
---|
645 | else
|
---|
646 | {
|
---|
647 | destColor.Auto = (bool)value;
|
---|
648 | }
|
---|
649 |
|
---|
650 | break;
|
---|
651 | default:
|
---|
652 | throw (new ArgumentException("Invalid class/property for class Fill."));
|
---|
653 | }
|
---|
654 | int subId;
|
---|
655 | string id = fill.Id;
|
---|
656 | subId = _styles.Fills.FindIndexByID(id);
|
---|
657 | if (subId == int.MinValue)
|
---|
658 | {
|
---|
659 | return _styles.Fills.Add(id, fill);
|
---|
660 | }
|
---|
661 | return subId;
|
---|
662 | }
|
---|
663 | private int GetIdGradientFill(eStyleClass styleClass, eStyleProperty styleProperty, object value)
|
---|
664 | {
|
---|
665 | ExcelGradientFillXml fill;
|
---|
666 | if(Fill is ExcelGradientFillXml)
|
---|
667 | {
|
---|
668 | fill = (ExcelGradientFillXml)Fill.Copy();
|
---|
669 | }
|
---|
670 | else
|
---|
671 | {
|
---|
672 | fill = new ExcelGradientFillXml(Fill.NameSpaceManager);
|
---|
673 | fill.GradientColor1.SetColor(Color.White);
|
---|
674 | fill.GradientColor2.SetColor(Color.FromArgb(79,129,189));
|
---|
675 | fill.Type=ExcelFillGradientType.Linear;
|
---|
676 | fill.Degree=90;
|
---|
677 | fill.Top = double.NaN;
|
---|
678 | fill.Bottom = double.NaN;
|
---|
679 | fill.Left = double.NaN;
|
---|
680 | fill.Right = double.NaN;
|
---|
681 | }
|
---|
682 |
|
---|
683 | switch (styleProperty)
|
---|
684 | {
|
---|
685 | case eStyleProperty.GradientType:
|
---|
686 | fill.Type = (ExcelFillGradientType)value;
|
---|
687 | break;
|
---|
688 | case eStyleProperty.GradientDegree:
|
---|
689 | fill.Degree = (double)value;
|
---|
690 | break;
|
---|
691 | case eStyleProperty.GradientTop:
|
---|
692 | fill.Top = (double)value;
|
---|
693 | break;
|
---|
694 | case eStyleProperty.GradientBottom:
|
---|
695 | fill.Bottom = (double)value;
|
---|
696 | break;
|
---|
697 | case eStyleProperty.GradientLeft:
|
---|
698 | fill.Left = (double)value;
|
---|
699 | break;
|
---|
700 | case eStyleProperty.GradientRight:
|
---|
701 | fill.Right = (double)value;
|
---|
702 | break;
|
---|
703 | case eStyleProperty.Color:
|
---|
704 | case eStyleProperty.Tint:
|
---|
705 | case eStyleProperty.IndexedColor:
|
---|
706 | case eStyleProperty.AutoColor:
|
---|
707 | ExcelColorXml destColor;
|
---|
708 |
|
---|
709 | if (styleClass == eStyleClass.FillGradientColor1)
|
---|
710 | {
|
---|
711 | destColor = fill.GradientColor1;
|
---|
712 | }
|
---|
713 | else
|
---|
714 | {
|
---|
715 | destColor = fill.GradientColor2;
|
---|
716 | }
|
---|
717 |
|
---|
718 | if (styleProperty == eStyleProperty.Color)
|
---|
719 | {
|
---|
720 | destColor.Rgb = value.ToString();
|
---|
721 | }
|
---|
722 | else if (styleProperty == eStyleProperty.Tint)
|
---|
723 | {
|
---|
724 | destColor.Tint = (decimal)value;
|
---|
725 | }
|
---|
726 | else if (styleProperty == eStyleProperty.IndexedColor)
|
---|
727 | {
|
---|
728 | destColor.Indexed = (int)value;
|
---|
729 | }
|
---|
730 | else
|
---|
731 | {
|
---|
732 | destColor.Auto = (bool)value;
|
---|
733 | }
|
---|
734 | break;
|
---|
735 | default:
|
---|
736 | throw (new ArgumentException("Invalid class/property for class Fill."));
|
---|
737 | }
|
---|
738 | int subId;
|
---|
739 | string id = fill.Id;
|
---|
740 | subId = _styles.Fills.FindIndexByID(id);
|
---|
741 | if (subId == int.MinValue)
|
---|
742 | {
|
---|
743 | return _styles.Fills.Add(id, fill);
|
---|
744 | }
|
---|
745 | return subId;
|
---|
746 | }
|
---|
747 |
|
---|
748 | private int GetIdNumberFormat(eStyleProperty styleProperty, object value)
|
---|
749 | {
|
---|
750 | if (styleProperty == eStyleProperty.Format)
|
---|
751 | {
|
---|
752 | ExcelNumberFormatXml item=null;
|
---|
753 | if (!_styles.NumberFormats.FindByID(value.ToString(), ref item))
|
---|
754 | {
|
---|
755 | item = new ExcelNumberFormatXml(NameSpaceManager) { Format = value.ToString(), NumFmtId = _styles.NumberFormats.NextId++ };
|
---|
756 | _styles.NumberFormats.Add(value.ToString(), item);
|
---|
757 | }
|
---|
758 | return item.NumFmtId;
|
---|
759 | }
|
---|
760 | else
|
---|
761 | {
|
---|
762 | throw (new Exception("Invalid property for class Numberformat"));
|
---|
763 | }
|
---|
764 | }
|
---|
765 | private int GetIdFont(eStyleProperty styleProperty, object value)
|
---|
766 | {
|
---|
767 | ExcelFontXml fnt = Font.Copy();
|
---|
768 |
|
---|
769 | switch (styleProperty)
|
---|
770 | {
|
---|
771 | case eStyleProperty.Name:
|
---|
772 | fnt.Name = value.ToString();
|
---|
773 | break;
|
---|
774 | case eStyleProperty.Size:
|
---|
775 | fnt.Size = (float)value;
|
---|
776 | break;
|
---|
777 | case eStyleProperty.Family:
|
---|
778 | fnt.Family = (int)value;
|
---|
779 | break;
|
---|
780 | case eStyleProperty.Bold:
|
---|
781 | fnt.Bold = (bool)value;
|
---|
782 | break;
|
---|
783 | case eStyleProperty.Italic:
|
---|
784 | fnt.Italic = (bool)value;
|
---|
785 | break;
|
---|
786 | case eStyleProperty.Strike:
|
---|
787 | fnt.Strike = (bool)value;
|
---|
788 | break;
|
---|
789 | case eStyleProperty.UnderlineType:
|
---|
790 | fnt.UnderLineType = (ExcelUnderLineType)value;
|
---|
791 | break;
|
---|
792 | case eStyleProperty.Color:
|
---|
793 | fnt.Color.Rgb=value.ToString();
|
---|
794 | break;
|
---|
795 | case eStyleProperty.VerticalAlign:
|
---|
796 | fnt.VerticalAlign = ((ExcelVerticalAlignmentFont)value) == ExcelVerticalAlignmentFont.None ? "" : value.ToString().ToLower(CultureInfo.InvariantCulture);
|
---|
797 | break;
|
---|
798 | default:
|
---|
799 | throw (new Exception("Invalid property for class Font"));
|
---|
800 | }
|
---|
801 | int subId;
|
---|
802 | string id = fnt.Id;
|
---|
803 | subId = _styles.Fonts.FindIndexByID(id);
|
---|
804 | if (subId == int.MinValue)
|
---|
805 | {
|
---|
806 | return _styles.Fonts.Add(id,fnt);
|
---|
807 | }
|
---|
808 | return subId;
|
---|
809 | }
|
---|
810 | internal override XmlNode CreateXmlNode(XmlNode topNode)
|
---|
811 | {
|
---|
812 | return CreateXmlNode(topNode, false);
|
---|
813 | }
|
---|
814 | internal XmlNode CreateXmlNode(XmlNode topNode, bool isCellStyleXsf)
|
---|
815 | {
|
---|
816 | TopNode = topNode;
|
---|
817 | var doSetXfId = (!isCellStyleXsf && _xfID > int.MinValue && _styles.CellStyleXfs.Count > 0 && _styles.CellStyleXfs[_xfID].newID > int.MinValue);
|
---|
818 | if (_numFmtId >= 0)
|
---|
819 | {
|
---|
820 | SetXmlNodeString("@numFmtId", _numFmtId.ToString());
|
---|
821 | if(doSetXfId) SetXmlNodeString("@applyNumberFormat", "1");
|
---|
822 | }
|
---|
823 | if (_fontId >= 0)
|
---|
824 | {
|
---|
825 | SetXmlNodeString("@fontId", _styles.Fonts[_fontId].newID.ToString());
|
---|
826 | if (doSetXfId) SetXmlNodeString("@applyFont", "1");
|
---|
827 | }
|
---|
828 | if (_fillId >= 0)
|
---|
829 | {
|
---|
830 | SetXmlNodeString("@fillId", _styles.Fills[_fillId].newID.ToString());
|
---|
831 | if (doSetXfId) SetXmlNodeString("@applyFill", "1");
|
---|
832 | }
|
---|
833 | if (_borderId >= 0)
|
---|
834 | {
|
---|
835 | SetXmlNodeString("@borderId", _styles.Borders[_borderId].newID.ToString());
|
---|
836 | if (doSetXfId) SetXmlNodeString("@applyBorder", "1");
|
---|
837 | }
|
---|
838 | if(_horizontalAlignment != ExcelHorizontalAlignment.General) this.SetXmlNodeString(horizontalAlignPath, SetAlignString(_horizontalAlignment));
|
---|
839 | if (doSetXfId)
|
---|
840 | {
|
---|
841 | SetXmlNodeString("@xfId", _styles.CellStyleXfs[_xfID].newID.ToString());
|
---|
842 | }
|
---|
843 | if (_verticalAlignment != ExcelVerticalAlignment.Bottom) this.SetXmlNodeString(verticalAlignPath, SetAlignString(_verticalAlignment));
|
---|
844 | if(_wrapText) this.SetXmlNodeString(wrapTextPath, "1");
|
---|
845 | if(_readingOrder!=ExcelReadingOrder.ContextDependent) this.SetXmlNodeString(readingOrderPath, ((int)_readingOrder).ToString());
|
---|
846 | if (_shrinkToFit) this.SetXmlNodeString(shrinkToFitPath, "1");
|
---|
847 | if (_indent > 0) SetXmlNodeString(indentPath, _indent.ToString());
|
---|
848 | if (_textRotation > 0) this.SetXmlNodeString(textRotationPath, _textRotation.ToString());
|
---|
849 | if (!_locked) this.SetXmlNodeString(lockedPath, "0");
|
---|
850 | if (_hidden) this.SetXmlNodeString(hiddenPath, "1");
|
---|
851 | return TopNode;
|
---|
852 | }
|
---|
853 |
|
---|
854 | private string SetAlignString(Enum align)
|
---|
855 | {
|
---|
856 | string newName = Enum.GetName(align.GetType(), align);
|
---|
857 | return newName.Substring(0, 1).ToLower(CultureInfo.InvariantCulture) + newName.Substring(1, newName.Length - 1);
|
---|
858 | }
|
---|
859 | }
|
---|
860 | } |
---|