Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Table/ExcelTableCollection.cs @ 18172

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

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 6.4 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    Added   30-AUG-2010
30 * Jan Källman    License changed GPL-->LGPL 2011-12-16
31 *******************************************************************************/
32using System;
33using System.Collections.Generic;
34using System.Text;
35using System.Xml;
36
37namespace OfficeOpenXml.Table
38{
39    /// <summary>
40    /// A collection of table objects
41    /// </summary>
42    public class ExcelTableCollection : IEnumerable<ExcelTable>
43    {
44        List<ExcelTable> _tables = new List<ExcelTable>();
45        internal Dictionary<string, int> _tableNames = new Dictionary<string, int>();
46        ExcelWorksheet _ws;       
47        internal ExcelTableCollection(ExcelWorksheet ws)
48        {
49            var pck = ws._package.Package;
50            _ws = ws;
51            foreach(XmlElement node in ws.WorksheetXml.SelectNodes("//d:tableParts/d:tablePart", ws.NameSpaceManager))
52            {
53                var rel = ws.Part.GetRelationship(node.GetAttribute("id",ExcelPackage.schemaRelationships));
54                var tbl = new ExcelTable(rel, ws);
55                if (tbl.Id + 1 > _ws.Workbook._nextTableID) _ws.Workbook._nextTableID = tbl.Id + 1;
56                _tableNames.Add(tbl.Name, _tables.Count);
57                _tables.Add(tbl);
58            }
59        }
60        private ExcelTable Add(ExcelTable tbl)
61        {
62            _tables.Add(tbl);
63            _tableNames.Add(tbl.Name, _tables.Count - 1);
64            if (tbl.Id >= _ws.Workbook._nextTableID)
65            {
66                _ws.Workbook._nextTableID = tbl.Id + 1;
67            }
68            return tbl;
69        }
70
71        /// <summary>
72        /// Create a table on the supplied range
73        /// </summary>
74        /// <param name="Range">The range address including header and total row</param>
75        /// <param name="Name">The name of the table. Must be unique </param>
76        /// <returns>The table object</returns>
77        public ExcelTable Add(ExcelAddressBase Range, string Name)
78        {
79            if (string.IsNullOrEmpty(Name))
80            {
81                Name = GetNewTableName();
82            }
83            else if (_ws.Workbook.ExistsTableName(Name))
84            {
85                throw (new ArgumentException("Tablename is not unique"));
86            }
87            foreach (var t in _tables)
88            {
89                if (t.Address.Collide(Range) != ExcelAddressBase.eAddressCollition.No)
90                {
91                    throw (new ArgumentException(string.Format("Table range collides with table {0}", t.Name)));
92                }
93            }
94            return Add(new ExcelTable(_ws, Range, Name, _ws.Workbook._nextTableID));
95        }
96
97        internal string GetNewTableName()
98        {
99            string name = "Table1";
100            int i = 2;
101            while (_ws.Workbook.ExistsTableName(name))
102            {
103                name = string.Format("Table{0}", i++);
104            }
105            return name;
106        }
107        /// <summary>
108        /// Number of items in the collection
109        /// </summary>
110        public int Count
111        {
112            get
113            {
114                return _tables.Count;
115            }
116        }
117        /// <summary>
118        /// Get the table object from a range.
119        /// </summary>
120        /// <param name="Range">The range</param>
121        /// <returns>The table. Null if no range matches</returns>
122        public ExcelTable GetFromRange(ExcelRangeBase Range)
123        {
124            foreach (var tbl in Range.Worksheet.Tables)
125            {
126                if (tbl.Address._address == Range._address)
127                {
128                    return tbl;
129                }
130            }
131            return null;
132        }
133        /// <summary>
134        /// The table Index. Base 0.
135        /// </summary>
136        /// <param name="Index"></param>
137        /// <returns></returns>
138        public ExcelTable this[int Index]
139        {
140            get
141            {
142                if (Index < 0 || Index >= _tables.Count)
143                {
144                    throw (new ArgumentOutOfRangeException("Table index out of range"));
145                }
146                return _tables[Index];
147            }
148        }
149        /// <summary>
150        /// Indexer
151        /// </summary>
152        /// <param name="Name">The name of the table</param>
153        /// <returns>The table. Null if the table name is not found in the collection</returns>
154        public ExcelTable this[string Name]
155        {
156            get
157            {
158                if (_tableNames.ContainsKey(Name))
159                {
160                    return _tables[_tableNames[Name]];
161                }
162                else
163                {
164                    return null;
165                }
166            }
167        }
168        public IEnumerator<ExcelTable> GetEnumerator()
169        {
170            return _tables.GetEnumerator();
171        }
172
173        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
174        {
175            return _tables.GetEnumerator();
176        }
177    }
178}
Note: See TracBrowser for help on using the repository browser.