Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/3.1.3/EPPlus-3.1.3/Table/ExcelTableCollection.cs @ 11733

Last change on this file since 11733 was 9580, checked in by sforsten, 12 years ago

#1730:

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