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 21-MAR-2011
|
---|
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.IO.Packaging;
|
---|
36 | using System.Xml;
|
---|
37 |
|
---|
38 | namespace OfficeOpenXml.Table.PivotTable
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// A collection of pivottable objects
|
---|
42 | /// </summary>
|
---|
43 | public class ExcelPivotTableCollection : IEnumerable<ExcelPivotTable>
|
---|
44 | {
|
---|
45 | List<ExcelPivotTable> _pivotTables = new List<ExcelPivotTable>();
|
---|
46 | internal Dictionary<string, int> _pivotTableNames = new Dictionary<string, int>();
|
---|
47 | ExcelWorksheet _ws;
|
---|
48 | internal ExcelPivotTableCollection(ExcelWorksheet ws)
|
---|
49 | {
|
---|
50 | Package pck = ws._package.Package;
|
---|
51 | _ws = ws;
|
---|
52 | foreach(var rel in ws.Part.GetRelationships())
|
---|
53 | {
|
---|
54 | if (rel.RelationshipType == ExcelPackage.schemaRelationships + "/pivotTable")
|
---|
55 | {
|
---|
56 | var tbl = new ExcelPivotTable(rel, ws);
|
---|
57 | _pivotTableNames.Add(tbl.Name, _pivotTables.Count);
|
---|
58 | if (tbl.Id + 1 > _ws.Workbook._nextPivotTableID) _ws.Workbook._nextPivotTableID = tbl.Id + 1;
|
---|
59 | _pivotTables.Add(tbl);
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|
63 | private ExcelPivotTable Add(ExcelPivotTable tbl)
|
---|
64 | {
|
---|
65 | _pivotTables.Add(tbl);
|
---|
66 | _pivotTableNames.Add(tbl.Name, _pivotTables.Count - 1);
|
---|
67 | if (tbl.Id >= _ws.Workbook._nextPivotTableID)
|
---|
68 | {
|
---|
69 | _ws.Workbook._nextPivotTableID = tbl.Id + 1;
|
---|
70 | }
|
---|
71 | return tbl;
|
---|
72 | }
|
---|
73 |
|
---|
74 | /// <summary>
|
---|
75 | /// Create a pivottable on the supplied range
|
---|
76 | /// </summary>
|
---|
77 | /// <param name="Range">The range address including header and total row</param>
|
---|
78 | /// <param name="Source">The Source data range address</param>
|
---|
79 | /// <param name="Name">The name of the table. Must be unique </param>
|
---|
80 | /// <returns>The pivottable object</returns>
|
---|
81 | public ExcelPivotTable Add(ExcelAddressBase Range, ExcelRangeBase Source, string Name)
|
---|
82 | {
|
---|
83 | if (string.IsNullOrEmpty(Name))
|
---|
84 | {
|
---|
85 | Name = GetNewTableName();
|
---|
86 | }
|
---|
87 | if (Range.WorkSheet != _ws.Name)
|
---|
88 | {
|
---|
89 | throw(new Exception("The Range must be in the current worksheet"));
|
---|
90 | }
|
---|
91 | else if (_ws.Workbook.ExistsTableName(Name))
|
---|
92 | {
|
---|
93 | throw (new ArgumentException("Tablename is not unique"));
|
---|
94 | }
|
---|
95 | foreach (var t in _pivotTables)
|
---|
96 | {
|
---|
97 | if (t.Address.Collide(Range) != ExcelAddressBase.eAddressCollition.No)
|
---|
98 | {
|
---|
99 | throw (new ArgumentException(string.Format("Table range collides with table {0}", t.Name)));
|
---|
100 | }
|
---|
101 | }
|
---|
102 | return Add(new ExcelPivotTable(_ws, Range, Source, Name, _ws.Workbook._nextPivotTableID++));
|
---|
103 | }
|
---|
104 |
|
---|
105 | internal string GetNewTableName()
|
---|
106 | {
|
---|
107 | string name = "Pivottable1";
|
---|
108 | int i = 2;
|
---|
109 | while (_ws.Workbook.ExistsPivotTableName(name))
|
---|
110 | {
|
---|
111 | name = string.Format("Pivottable{0}", i++);
|
---|
112 | }
|
---|
113 | return name;
|
---|
114 | }
|
---|
115 | public int Count
|
---|
116 | {
|
---|
117 | get
|
---|
118 | {
|
---|
119 | return _pivotTables.Count;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | /// <summary>
|
---|
123 | /// The pivottable Index. Base 0.
|
---|
124 | /// </summary>
|
---|
125 | /// <param name="Index"></param>
|
---|
126 | /// <returns></returns>
|
---|
127 | public ExcelPivotTable this[int Index]
|
---|
128 | {
|
---|
129 | get
|
---|
130 | {
|
---|
131 | if (Index < 0 || Index >= _pivotTables.Count)
|
---|
132 | {
|
---|
133 | throw (new ArgumentOutOfRangeException("PivotTable index out of range"));
|
---|
134 | }
|
---|
135 | return _pivotTables[Index];
|
---|
136 | }
|
---|
137 | }
|
---|
138 | /// <summary>
|
---|
139 | /// Pivottabes accesed by name
|
---|
140 | /// </summary>
|
---|
141 | /// <param name="Name">The name of the pivottable</param>
|
---|
142 | /// <returns>The Pivotable. Null if the no match is found</returns>
|
---|
143 | public ExcelPivotTable this[string Name]
|
---|
144 | {
|
---|
145 | get
|
---|
146 | {
|
---|
147 | if (_pivotTableNames.ContainsKey(Name))
|
---|
148 | {
|
---|
149 | return _pivotTables[_pivotTableNames[Name]];
|
---|
150 | }
|
---|
151 | else
|
---|
152 | {
|
---|
153 | return null;
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | public IEnumerator<ExcelPivotTable> GetEnumerator()
|
---|
158 | {
|
---|
159 | return _pivotTables.GetEnumerator();
|
---|
160 | }
|
---|
161 |
|
---|
162 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
---|
163 | {
|
---|
164 | return _pivotTables.GetEnumerator();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|