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