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 | * Richard Tallent Fix inadvertent removal of XML node 2012-10-31
|
---|
32 | * Richard Tallent Remove VertAlign node if no alignment specified 2012-10-31
|
---|
33 | *******************************************************************************/
|
---|
34 | using System;
|
---|
35 | using System.Collections.Generic;
|
---|
36 | using System.Text;
|
---|
37 | using System.Xml;
|
---|
38 | using System.Drawing;
|
---|
39 | using System.Globalization;
|
---|
40 |
|
---|
41 | namespace OfficeOpenXml.Style
|
---|
42 | {
|
---|
43 | /// <summary>
|
---|
44 | /// A richtext part
|
---|
45 | /// </summary>
|
---|
46 | public class ExcelRichText : XmlHelper
|
---|
47 | {
|
---|
48 | internal ExcelRichText(XmlNamespaceManager ns, XmlNode topNode) :
|
---|
49 | base(ns, topNode)
|
---|
50 | {
|
---|
51 | SchemaNodeOrder=new string[] {"rPr", "t", "b", "i","strike", "u", "vertAlign" , "sz", "color", "rFont", "family", "scheme", "charset"};
|
---|
52 | }
|
---|
53 | internal delegate void CallbackDelegate();
|
---|
54 | CallbackDelegate _callback;
|
---|
55 | internal void SetCallback(CallbackDelegate callback)
|
---|
56 | {
|
---|
57 | _callback=callback;
|
---|
58 | }
|
---|
59 | const string TEXT_PATH="d:t";
|
---|
60 | /// <summary>
|
---|
61 | /// The text
|
---|
62 | /// </summary>
|
---|
63 | public string Text
|
---|
64 | {
|
---|
65 |
|
---|
66 | get
|
---|
67 | {
|
---|
68 | return GetXmlNodeString(TEXT_PATH);
|
---|
69 | }
|
---|
70 | set
|
---|
71 | {
|
---|
72 | // Don't remove if blank -- setting a blank rich text value on a node is common,
|
---|
73 | // for example when applying both bold and italic to text.
|
---|
74 | SetXmlNodeString(TEXT_PATH, value, false);
|
---|
75 | if (PreserveSpace)
|
---|
76 | {
|
---|
77 | XmlElement elem = TopNode.SelectSingleNode(TEXT_PATH, NameSpaceManager) as XmlElement;
|
---|
78 | elem.SetAttribute("xml:space", "preserve");
|
---|
79 | }
|
---|
80 | if (_callback != null) _callback();
|
---|
81 | }
|
---|
82 | }
|
---|
83 | /// <summary>
|
---|
84 | /// Preserves whitespace. Default true
|
---|
85 | /// </summary>
|
---|
86 | public bool PreserveSpace
|
---|
87 | {
|
---|
88 | get
|
---|
89 | {
|
---|
90 | XmlElement elem = TopNode.SelectSingleNode(TEXT_PATH, NameSpaceManager) as XmlElement;
|
---|
91 | if (elem != null)
|
---|
92 | {
|
---|
93 | return elem.GetAttribute("xml:space")=="preserve";
|
---|
94 | }
|
---|
95 | return false;
|
---|
96 | }
|
---|
97 | set
|
---|
98 | {
|
---|
99 | XmlElement elem = TopNode.SelectSingleNode(TEXT_PATH, NameSpaceManager) as XmlElement;
|
---|
100 | if (elem != null)
|
---|
101 | {
|
---|
102 | if (value)
|
---|
103 | {
|
---|
104 | elem.SetAttribute("xml:space", "preserve");
|
---|
105 | }
|
---|
106 | else
|
---|
107 | {
|
---|
108 | elem.RemoveAttribute("xml:space");
|
---|
109 | }
|
---|
110 | }
|
---|
111 | if (_callback != null) _callback();
|
---|
112 | }
|
---|
113 | }
|
---|
114 | const string BOLD_PATH = "d:rPr/d:b";
|
---|
115 | /// <summary>
|
---|
116 | /// Bold text
|
---|
117 | /// </summary>
|
---|
118 | public bool Bold
|
---|
119 | {
|
---|
120 | get
|
---|
121 | {
|
---|
122 | return ExistNode(BOLD_PATH);
|
---|
123 | }
|
---|
124 | set
|
---|
125 | {
|
---|
126 | if (value)
|
---|
127 | {
|
---|
128 | CreateNode(BOLD_PATH);
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | DeleteNode(BOLD_PATH);
|
---|
133 | }
|
---|
134 | if(_callback!=null) _callback();
|
---|
135 | }
|
---|
136 | }
|
---|
137 | const string ITALIC_PATH = "d:rPr/d:i";
|
---|
138 | /// <summary>
|
---|
139 | /// Italic text
|
---|
140 | /// </summary>
|
---|
141 | public bool Italic
|
---|
142 | {
|
---|
143 | get
|
---|
144 | {
|
---|
145 | //return GetXmlNodeBool(ITALIC_PATH, false);
|
---|
146 | return ExistNode(ITALIC_PATH);
|
---|
147 | }
|
---|
148 | set
|
---|
149 | {
|
---|
150 | if (value)
|
---|
151 | {
|
---|
152 | CreateNode(ITALIC_PATH);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | DeleteNode(ITALIC_PATH);
|
---|
157 | }
|
---|
158 | if (_callback != null) _callback();
|
---|
159 | }
|
---|
160 | }
|
---|
161 | const string STRIKE_PATH = "d:rPr/d:strike";
|
---|
162 | /// <summary>
|
---|
163 | /// Strike-out text
|
---|
164 | /// </summary>
|
---|
165 | public bool Strike
|
---|
166 | {
|
---|
167 | get
|
---|
168 | {
|
---|
169 | return ExistNode(STRIKE_PATH);
|
---|
170 | }
|
---|
171 | set
|
---|
172 | {
|
---|
173 | if (value)
|
---|
174 | {
|
---|
175 | CreateNode(STRIKE_PATH);
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | DeleteNode(STRIKE_PATH);
|
---|
180 | }
|
---|
181 | if (_callback != null) _callback();
|
---|
182 | }
|
---|
183 | }
|
---|
184 | const string UNDERLINE_PATH = "d:rPr/d:u";
|
---|
185 | /// <summary>
|
---|
186 | /// Underlined text
|
---|
187 | /// </summary>
|
---|
188 | public bool UnderLine
|
---|
189 | {
|
---|
190 | get
|
---|
191 | {
|
---|
192 | return ExistNode(UNDERLINE_PATH);
|
---|
193 | }
|
---|
194 | set
|
---|
195 | {
|
---|
196 | if (value)
|
---|
197 | {
|
---|
198 | CreateNode(UNDERLINE_PATH);
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | DeleteNode(UNDERLINE_PATH);
|
---|
203 | }
|
---|
204 | if (_callback != null) _callback();
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | const string VERT_ALIGN_PATH = "d:rPr/d:vertAlign/@val";
|
---|
209 | /// <summary>
|
---|
210 | /// Vertical Alignment
|
---|
211 | /// </summary>
|
---|
212 | public ExcelVerticalAlignmentFont VerticalAlign
|
---|
213 | {
|
---|
214 | get
|
---|
215 | {
|
---|
216 | string v=GetXmlNodeString(VERT_ALIGN_PATH);
|
---|
217 | if(v=="")
|
---|
218 | {
|
---|
219 | return ExcelVerticalAlignmentFont.None;
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | try
|
---|
224 | {
|
---|
225 | return (ExcelVerticalAlignmentFont)Enum.Parse(typeof(ExcelVerticalAlignmentFont), v, true);
|
---|
226 | }
|
---|
227 | catch
|
---|
228 | {
|
---|
229 | return ExcelVerticalAlignmentFont.None;
|
---|
230 | }
|
---|
231 | }
|
---|
232 | }
|
---|
233 | set
|
---|
234 | {
|
---|
235 | if(value == ExcelVerticalAlignmentFont.None) {
|
---|
236 | // If Excel 2010 encounters a vertical align value of blank, it will not load
|
---|
237 | // the spreadsheet. So if None is specified, delete the node, it will be
|
---|
238 | // recreated if a new value is applied later.
|
---|
239 | DeleteNode(VERT_ALIGN_PATH);
|
---|
240 | } else {
|
---|
241 | SetXmlNodeString(VERT_ALIGN_PATH, value.ToString().ToLowerInvariant());
|
---|
242 | }
|
---|
243 | if (_callback != null) _callback();
|
---|
244 | }
|
---|
245 | }
|
---|
246 | const string SIZE_PATH = "d:rPr/d:sz/@val";
|
---|
247 | /// <summary>
|
---|
248 | /// Font size
|
---|
249 | /// </summary>
|
---|
250 | public float Size
|
---|
251 | {
|
---|
252 | get
|
---|
253 | {
|
---|
254 | return Convert.ToSingle(GetXmlNodeDecimal(SIZE_PATH));
|
---|
255 | }
|
---|
256 | set
|
---|
257 | {
|
---|
258 | SetXmlNodeString(SIZE_PATH, value.ToString(CultureInfo.InvariantCulture));
|
---|
259 | if (_callback != null) _callback();
|
---|
260 | }
|
---|
261 | }
|
---|
262 | const string FONT_PATH = "d:rPr/d:rFont/@val";
|
---|
263 | /// <summary>
|
---|
264 | /// Name of the font
|
---|
265 | /// </summary>
|
---|
266 | public string FontName
|
---|
267 | {
|
---|
268 | get
|
---|
269 | {
|
---|
270 | return GetXmlNodeString(FONT_PATH);
|
---|
271 | }
|
---|
272 | set
|
---|
273 | {
|
---|
274 | SetXmlNodeString(FONT_PATH, value);
|
---|
275 | if (_callback != null) _callback();
|
---|
276 | }
|
---|
277 | }
|
---|
278 | const string COLOR_PATH = "d:rPr/d:color/@rgb";
|
---|
279 | /// <summary>
|
---|
280 | /// Text color
|
---|
281 | /// </summary>
|
---|
282 | public Color Color
|
---|
283 | {
|
---|
284 | get
|
---|
285 | {
|
---|
286 | string col = GetXmlNodeString(COLOR_PATH);
|
---|
287 | if (col == "")
|
---|
288 | {
|
---|
289 | return Color.Empty;
|
---|
290 | }
|
---|
291 | else
|
---|
292 | {
|
---|
293 | return Color.FromArgb(int.Parse(col, System.Globalization.NumberStyles.AllowHexSpecifier));
|
---|
294 | }
|
---|
295 | }
|
---|
296 | set
|
---|
297 | {
|
---|
298 | SetXmlNodeString(COLOR_PATH, value.ToArgb().ToString("X")/*.Substring(2, 6)*/);
|
---|
299 | if (_callback != null) _callback();
|
---|
300 | }
|
---|
301 | }
|
---|
302 | }
|
---|
303 | }
|
---|