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 | namespace OfficeOpenXml.Style.XmlAccess
|
---|
37 | {
|
---|
38 | /// <summary>
|
---|
39 | /// Xml access class for fills
|
---|
40 | /// </summary>
|
---|
41 | public class ExcelFillXml : StyleXmlHelper
|
---|
42 | {
|
---|
43 | internal ExcelFillXml(XmlNamespaceManager nameSpaceManager)
|
---|
44 | : base(nameSpaceManager)
|
---|
45 | {
|
---|
46 | _fillPatternType = ExcelFillStyle.None;
|
---|
47 | _backgroundColor = new ExcelColorXml(NameSpaceManager);
|
---|
48 | _patternColor = new ExcelColorXml(NameSpaceManager);
|
---|
49 | }
|
---|
50 | internal ExcelFillXml(XmlNamespaceManager nsm, XmlNode topNode):
|
---|
51 | base(nsm, topNode)
|
---|
52 | {
|
---|
53 | PatternType = GetPatternType(GetXmlNodeString(fillPatternTypePath));
|
---|
54 | _backgroundColor = new ExcelColorXml(nsm, topNode.SelectSingleNode(_backgroundColorPath, nsm));
|
---|
55 | _patternColor = new ExcelColorXml(nsm, topNode.SelectSingleNode(_patternColorPath, nsm));
|
---|
56 | }
|
---|
57 |
|
---|
58 | private ExcelFillStyle GetPatternType(string patternType)
|
---|
59 | {
|
---|
60 | if (patternType == "") return ExcelFillStyle.None;
|
---|
61 | patternType = patternType.Substring(0, 1).ToUpper() + patternType.Substring(1, patternType.Length - 1);
|
---|
62 | try
|
---|
63 | {
|
---|
64 | return (ExcelFillStyle)Enum.Parse(typeof(ExcelFillStyle), patternType);
|
---|
65 | }
|
---|
66 | catch
|
---|
67 | {
|
---|
68 | return ExcelFillStyle.None;
|
---|
69 | }
|
---|
70 | }
|
---|
71 | internal override string Id
|
---|
72 | {
|
---|
73 | get
|
---|
74 | {
|
---|
75 | return PatternType + PatternColor.Id + BackgroundColor.Id;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | #region Public Properties
|
---|
79 | const string fillPatternTypePath = "d:patternFill/@patternType";
|
---|
80 | protected ExcelFillStyle _fillPatternType;
|
---|
81 | /// <summary>
|
---|
82 | /// Cell fill pattern style
|
---|
83 | /// </summary>
|
---|
84 | public ExcelFillStyle PatternType
|
---|
85 | {
|
---|
86 | get
|
---|
87 | {
|
---|
88 | return _fillPatternType;
|
---|
89 | }
|
---|
90 | set
|
---|
91 | {
|
---|
92 | _fillPatternType=value;
|
---|
93 | }
|
---|
94 | }
|
---|
95 | protected ExcelColorXml _patternColor = null;
|
---|
96 | const string _patternColorPath = "d:patternFill/d:bgColor";
|
---|
97 | /// <summary>
|
---|
98 | /// Pattern color
|
---|
99 | /// </summary>
|
---|
100 | public ExcelColorXml PatternColor
|
---|
101 | {
|
---|
102 | get
|
---|
103 | {
|
---|
104 | return _patternColor;
|
---|
105 | }
|
---|
106 | internal set
|
---|
107 | {
|
---|
108 | _patternColor = value;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | protected ExcelColorXml _backgroundColor = null;
|
---|
112 | const string _backgroundColorPath = "d:patternFill/d:fgColor";
|
---|
113 | /// <summary>
|
---|
114 | /// Cell background color
|
---|
115 | /// </summary>
|
---|
116 | public ExcelColorXml BackgroundColor
|
---|
117 | {
|
---|
118 | get
|
---|
119 | {
|
---|
120 | return _backgroundColor;
|
---|
121 | }
|
---|
122 | internal set
|
---|
123 | {
|
---|
124 | _backgroundColor=value;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 |
|
---|
130 | //internal Fill Copy()
|
---|
131 | //{
|
---|
132 | // Fill newFill = new Fill(NameSpaceManager, TopNode.Clone());
|
---|
133 | // return newFill;
|
---|
134 | //}
|
---|
135 |
|
---|
136 | internal virtual ExcelFillXml Copy()
|
---|
137 | {
|
---|
138 | ExcelFillXml newFill = new ExcelFillXml(NameSpaceManager);
|
---|
139 | newFill.PatternType = _fillPatternType;
|
---|
140 | newFill.BackgroundColor = _backgroundColor.Copy();
|
---|
141 | newFill.PatternColor = _patternColor.Copy();
|
---|
142 | return newFill;
|
---|
143 | }
|
---|
144 |
|
---|
145 | internal override XmlNode CreateXmlNode(XmlNode topNode)
|
---|
146 | {
|
---|
147 | TopNode = topNode;
|
---|
148 | SetXmlNodeString(fillPatternTypePath, SetPatternString(_fillPatternType));
|
---|
149 | if (PatternType != ExcelFillStyle.None)
|
---|
150 | {
|
---|
151 | XmlNode pattern = topNode.SelectSingleNode(fillPatternTypePath, NameSpaceManager);
|
---|
152 | if (BackgroundColor.Exists)
|
---|
153 | {
|
---|
154 | CreateNode(_backgroundColorPath);
|
---|
155 | BackgroundColor.CreateXmlNode(topNode.SelectSingleNode(_backgroundColorPath, NameSpaceManager));
|
---|
156 | if (PatternColor.Exists)
|
---|
157 | {
|
---|
158 | CreateNode(_patternColorPath);
|
---|
159 | //topNode.AppendChild(PatternColor.CreateXmlNode(topNode.SelectSingleNode(_patternColorPath, NameSpaceManager)));
|
---|
160 | PatternColor.CreateXmlNode(topNode.SelectSingleNode(_patternColorPath, NameSpaceManager));
|
---|
161 | }
|
---|
162 | }
|
---|
163 | }
|
---|
164 | return topNode;
|
---|
165 | }
|
---|
166 |
|
---|
167 | private string SetPatternString(ExcelFillStyle pattern)
|
---|
168 | {
|
---|
169 | string newName = Enum.GetName(typeof(ExcelFillStyle), pattern);
|
---|
170 | return newName.Substring(0, 1).ToLower() + newName.Substring(1, newName.Length - 1);
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|