Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2915-AbsoluteSymbol/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Style/ExcelParagraphCollection.cs @ 15943

Last change on this file since 15943 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 5.8 KB
Line 
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 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36using OfficeOpenXml.Drawing;
37using System.Drawing;
38
39namespace OfficeOpenXml.Style
40{
41    /// <summary>
42    /// A collection of Paragraph objects
43    /// </summary>
44    public class ExcelParagraphCollection : XmlHelper, IEnumerable<ExcelParagraph>
45    {
46        List<ExcelParagraph> _list = new List<ExcelParagraph>();
47        string _path;
48        internal ExcelParagraphCollection(XmlNamespaceManager ns, XmlNode topNode, string path, string[] schemaNodeOrder) :
49            base(ns, topNode)
50        {
51            var nl = topNode.SelectNodes(path + "/a:r", NameSpaceManager);
52            SchemaNodeOrder = schemaNodeOrder;
53            if (nl != null)
54            {
55                foreach (XmlNode n in nl)
56                {
57                    _list.Add(new ExcelParagraph(ns, n, "",schemaNodeOrder));
58                }
59            }
60            _path = path;
61        }
62        public ExcelParagraph this[int Index]
63        {
64            get
65            {
66                return _list[Index];
67            }
68        }
69        public int Count
70        {
71            get
72            {
73                return _list.Count;
74            }
75        }
76        /// <summary>
77        /// Add a rich text string
78        /// </summary>
79        /// <param name="Text">The text to add</param>
80        /// <returns></returns>
81        public ExcelParagraph Add(string Text)
82        {
83            XmlDocument doc;
84            if (TopNode is XmlDocument)
85            {
86                doc = TopNode as XmlDocument;
87            }
88            else
89            {
90                doc = TopNode.OwnerDocument;
91            }
92            XmlNode parentNode=TopNode.SelectSingleNode(_path, NameSpaceManager);
93            if (parentNode == null)
94            {
95                CreateNode(_path);
96                parentNode = TopNode.SelectSingleNode(_path, NameSpaceManager);
97            }
98           
99            var node = doc.CreateElement("a", "r", ExcelPackage.schemaDrawings);
100            parentNode.AppendChild(node);
101            var childNode = doc.CreateElement("a", "rPr", ExcelPackage.schemaDrawings);
102            node.AppendChild(childNode);
103            var rt = new ExcelParagraph(NameSpaceManager, node, "", SchemaNodeOrder);
104            rt.ComplexFont = "Calibri";
105            rt.LatinFont = "Calibri";
106            rt.Size = 11;
107
108            rt.Text = Text;
109            _list.Add(rt);
110            return rt;
111        }
112        public void Clear()
113        {
114            _list.Clear();
115            TopNode.RemoveAll();
116        }
117        public void RemoveAt(int Index)
118        {
119            var node = _list[Index].TopNode;
120            while (node != null && node.Name != "a:r")
121            {
122                node = node.ParentNode;
123            }
124            node.ParentNode.RemoveChild(node);
125            _list.RemoveAt(Index);
126        }
127        public void Remove(ExcelRichText Item)
128        {
129            TopNode.RemoveChild(Item.TopNode);
130        }
131        public string Text
132        {
133            get
134            {
135                StringBuilder sb = new StringBuilder();
136                foreach (var item in _list)
137                {
138                    sb.Append(item.Text);
139                }
140                return sb.ToString();
141            }
142            set
143            {
144                if (Count == 0)
145                {
146                    Add(value);
147                }
148                else
149                {
150                    this[0].Text = value;
151                    int count = Count;
152                    for (int ix = Count-1; ix > 0; ix--)
153                    {
154                        RemoveAt(ix);
155                    }
156                }
157            }
158        }
159        #region IEnumerable<ExcelRichText> Members
160
161        IEnumerator<ExcelParagraph> IEnumerable<ExcelParagraph>.GetEnumerator()
162        {
163            return _list.GetEnumerator();
164        }
165
166        #endregion
167
168        #region IEnumerable Members
169
170        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
171        {
172            return _list.GetEnumerator();
173        }
174
175        #endregion
176    }
177}
Note: See TracBrowser for help on using the repository browser.