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