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 |
|
---|
38 | namespace OfficeOpenXml.Style
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// Collection of Richtext objects
|
---|
42 | /// </summary>
|
---|
43 | public class ExcelRichTextCollection : XmlHelper, IEnumerable<ExcelRichText>
|
---|
44 | {
|
---|
45 | List<ExcelRichText> _list = new List<ExcelRichText>();
|
---|
46 | ExcelRangeBase _cells=null;
|
---|
47 | internal ExcelRichTextCollection(XmlNamespaceManager ns, XmlNode topNode) :
|
---|
48 | base(ns, topNode)
|
---|
49 | {
|
---|
50 | var nl = topNode.SelectNodes("d:r", NameSpaceManager);
|
---|
51 | if (nl != null)
|
---|
52 | {
|
---|
53 | foreach (XmlNode n in nl)
|
---|
54 | {
|
---|
55 | _list.Add(new ExcelRichText(ns, n));
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|
59 | internal ExcelRichTextCollection(XmlNamespaceManager ns, XmlNode topNode, ExcelRangeBase cells) :
|
---|
60 | this(ns, topNode)
|
---|
61 | {
|
---|
62 | _cells = cells;
|
---|
63 | }
|
---|
64 | /// <summary>
|
---|
65 | /// Collection containing the richtext objects
|
---|
66 | /// </summary>
|
---|
67 | /// <param name="Index"></param>
|
---|
68 | /// <returns></returns>
|
---|
69 | public ExcelRichText this[int Index]
|
---|
70 | {
|
---|
71 | get
|
---|
72 | {
|
---|
73 | var item=_list[Index];
|
---|
74 | if(_cells!=null) item.SetCallback(UpdateCells);
|
---|
75 | return item;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | /// <summary>
|
---|
79 | /// Items in the list
|
---|
80 | /// </summary>
|
---|
81 | public int Count
|
---|
82 | {
|
---|
83 | get
|
---|
84 | {
|
---|
85 | return _list.Count;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | /// <summary>
|
---|
89 | /// Add a rich text string
|
---|
90 | /// </summary>
|
---|
91 | /// <param name="Text">The text to add</param>
|
---|
92 | /// <returns></returns>
|
---|
93 | public ExcelRichText Add(string Text)
|
---|
94 | {
|
---|
95 | XmlDocument doc;
|
---|
96 | if (TopNode is XmlDocument)
|
---|
97 | {
|
---|
98 | doc = TopNode as XmlDocument;
|
---|
99 | }
|
---|
100 | else
|
---|
101 | {
|
---|
102 | doc = TopNode.OwnerDocument;
|
---|
103 | }
|
---|
104 | var node = doc.CreateElement("d", "r", ExcelPackage.schemaMain);
|
---|
105 | TopNode.AppendChild(node);
|
---|
106 | var rt = new ExcelRichText(NameSpaceManager, node);
|
---|
107 | if (_list.Count > 0)
|
---|
108 | {
|
---|
109 | ExcelRichText prevItem = _list[_list.Count - 1];
|
---|
110 | rt.FontName = prevItem.FontName;
|
---|
111 | rt.Size = prevItem.Size;
|
---|
112 | if (prevItem.Color.IsEmpty)
|
---|
113 | {
|
---|
114 | rt.Color = Color.Black;
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | rt.Color = prevItem.Color;
|
---|
119 | }
|
---|
120 | rt.PreserveSpace = rt.PreserveSpace;
|
---|
121 | rt.Bold = prevItem.Bold;
|
---|
122 | rt.Italic = prevItem.Italic;
|
---|
123 | rt.UnderLine = prevItem.UnderLine;
|
---|
124 | }
|
---|
125 | else if (_cells == null)
|
---|
126 | {
|
---|
127 | rt.FontName = "Calibri";
|
---|
128 | rt.Size = 11;
|
---|
129 | }
|
---|
130 | else
|
---|
131 | {
|
---|
132 | var style = _cells.Offset(0, 0).Style;
|
---|
133 | rt.FontName = style.Font.Name;
|
---|
134 | rt.Size = style.Font.Size;
|
---|
135 | rt.Bold = style.Font.Bold;
|
---|
136 | rt.Italic = style.Font.Italic;
|
---|
137 | _cells.IsRichText = true;
|
---|
138 | }
|
---|
139 | rt.Text = Text;
|
---|
140 | rt.PreserveSpace = true;
|
---|
141 | if(_cells!=null)
|
---|
142 | {
|
---|
143 | rt.SetCallback(UpdateCells);
|
---|
144 | UpdateCells();
|
---|
145 | }
|
---|
146 | _list.Add(rt);
|
---|
147 | return rt;
|
---|
148 | }
|
---|
149 | internal void UpdateCells()
|
---|
150 | {
|
---|
151 | _cells.SetValueRichText(TopNode.InnerXml);
|
---|
152 | }
|
---|
153 | /// <summary>
|
---|
154 | /// Clear the collection
|
---|
155 | /// </summary>
|
---|
156 | public void Clear()
|
---|
157 | {
|
---|
158 | _list.Clear();
|
---|
159 | TopNode.RemoveAll();
|
---|
160 | if (_cells != null) _cells.IsRichText = false;
|
---|
161 | }
|
---|
162 | /// <summary>
|
---|
163 | /// Removes an item at the specific index
|
---|
164 | /// </summary>
|
---|
165 | /// <param name="Index"></param>
|
---|
166 | public void RemoveAt(int Index)
|
---|
167 | {
|
---|
168 | TopNode.RemoveChild(_list[Index].TopNode);
|
---|
169 | _list.RemoveAt(Index);
|
---|
170 | if (_cells != null && _list.Count==0) _cells.IsRichText = false;
|
---|
171 | }
|
---|
172 | /// <summary>
|
---|
173 | /// Removes an item
|
---|
174 | /// </summary>
|
---|
175 | /// <param name="Item"></param>
|
---|
176 | public void Remove(ExcelRichText Item)
|
---|
177 | {
|
---|
178 | TopNode.RemoveChild(Item.TopNode);
|
---|
179 | _list.Remove(Item);
|
---|
180 | if (_cells != null && _list.Count == 0) _cells.IsRichText = false;
|
---|
181 | }
|
---|
182 | //public void Insert(int index, string Text)
|
---|
183 | //{
|
---|
184 | // _list.Insert(index, item);
|
---|
185 | //}
|
---|
186 |
|
---|
187 | /// <summary>
|
---|
188 | /// The text
|
---|
189 | /// </summary>
|
---|
190 | public string Text
|
---|
191 | {
|
---|
192 | get
|
---|
193 | {
|
---|
194 | StringBuilder sb=new StringBuilder();
|
---|
195 | foreach (var item in _list)
|
---|
196 | {
|
---|
197 | sb.Append(item.Text);
|
---|
198 | }
|
---|
199 | return sb.ToString();
|
---|
200 | }
|
---|
201 | set
|
---|
202 | {
|
---|
203 | if (Count == 0)
|
---|
204 | {
|
---|
205 | Add(value);
|
---|
206 | }
|
---|
207 | else
|
---|
208 | {
|
---|
209 | this[0].Text = value;
|
---|
210 | for (int ix = 1; ix < Count; ix++)
|
---|
211 | {
|
---|
212 | RemoveAt(ix);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 | #region IEnumerable<ExcelRichText> Members
|
---|
218 |
|
---|
219 | IEnumerator<ExcelRichText> IEnumerable<ExcelRichText>.GetEnumerator()
|
---|
220 | {
|
---|
221 | return _list.GetEnumerator();
|
---|
222 | }
|
---|
223 |
|
---|
224 | #endregion
|
---|
225 |
|
---|
226 | #region IEnumerable Members
|
---|
227 |
|
---|
228 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
229 | {
|
---|
230 | return _list.GetEnumerator();
|
---|
231 | }
|
---|
232 |
|
---|
233 | #endregion
|
---|
234 | }
|
---|
235 | }
|
---|