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