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.Globalization;
|
---|
35 | using System.Text;
|
---|
36 | using System.Xml;
|
---|
37 | using OfficeOpenXml.Style.XmlAccess;
|
---|
38 |
|
---|
39 | namespace OfficeOpenXml.Table.PivotTable
|
---|
40 | {
|
---|
41 | /// <summary>
|
---|
42 | /// A pivo table data field
|
---|
43 | /// </summary>
|
---|
44 | public class ExcelPivotTableDataField : XmlHelper
|
---|
45 | {
|
---|
46 | internal ExcelPivotTableDataField(XmlNamespaceManager ns, XmlNode topNode,ExcelPivotTableField field) :
|
---|
47 | base(ns, topNode)
|
---|
48 | {
|
---|
49 | if (topNode.Attributes.Count == 0)
|
---|
50 | {
|
---|
51 | Index = field.Index;
|
---|
52 | BaseField = 0;
|
---|
53 | BaseItem = 0;
|
---|
54 | }
|
---|
55 |
|
---|
56 | Field = field;
|
---|
57 | }
|
---|
58 | /// <summary>
|
---|
59 | /// The field
|
---|
60 | /// </summary>
|
---|
61 | public ExcelPivotTableField Field
|
---|
62 | {
|
---|
63 | get;
|
---|
64 | private set;
|
---|
65 | }
|
---|
66 | /// <summary>
|
---|
67 | /// The index of the datafield
|
---|
68 | /// </summary>
|
---|
69 | public int Index
|
---|
70 | {
|
---|
71 | get
|
---|
72 | {
|
---|
73 | return GetXmlNodeInt("@fld");
|
---|
74 | }
|
---|
75 | internal set
|
---|
76 | {
|
---|
77 | SetXmlNodeString("@fld",value.ToString());
|
---|
78 | }
|
---|
79 | }
|
---|
80 | /// <summary>
|
---|
81 | /// The name of the datafield
|
---|
82 | /// </summary>
|
---|
83 | public string Name
|
---|
84 | {
|
---|
85 | get
|
---|
86 | {
|
---|
87 | return GetXmlNodeString("@name");
|
---|
88 | }
|
---|
89 | set
|
---|
90 | {
|
---|
91 | if (Field._table.DataFields.ExistsDfName(value, this))
|
---|
92 | {
|
---|
93 | throw (new InvalidOperationException("Duplicate datafield name"));
|
---|
94 | }
|
---|
95 | SetXmlNodeString("@name", value);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | /// <summary>
|
---|
99 | /// Field index. Reference to the field collection
|
---|
100 | /// </summary>
|
---|
101 | public int BaseField
|
---|
102 | {
|
---|
103 | get
|
---|
104 | {
|
---|
105 | return GetXmlNodeInt("@baseField");
|
---|
106 | }
|
---|
107 | set
|
---|
108 | {
|
---|
109 | SetXmlNodeString("@baseField", value.ToString());
|
---|
110 | }
|
---|
111 | }
|
---|
112 | /// <summary>
|
---|
113 | /// Specifies the index to the base item when the ShowDataAs calculation is in use
|
---|
114 | /// </summary>
|
---|
115 | public int BaseItem
|
---|
116 | {
|
---|
117 | get
|
---|
118 | {
|
---|
119 | return GetXmlNodeInt("@baseItem");
|
---|
120 | }
|
---|
121 | set
|
---|
122 | {
|
---|
123 | SetXmlNodeString("@baseItem", value.ToString());
|
---|
124 | }
|
---|
125 | }
|
---|
126 | /// <summary>
|
---|
127 | /// Number format id.
|
---|
128 | /// </summary>
|
---|
129 | internal int NumFmtId
|
---|
130 | {
|
---|
131 | get
|
---|
132 | {
|
---|
133 | return GetXmlNodeInt("@numFmtId");
|
---|
134 | }
|
---|
135 | set
|
---|
136 | {
|
---|
137 | SetXmlNodeString("@numFmtId", value.ToString());
|
---|
138 | }
|
---|
139 | }
|
---|
140 | /// <summary>
|
---|
141 | /// Number format for the data column
|
---|
142 | /// </summary>
|
---|
143 | public string Format
|
---|
144 | {
|
---|
145 | get
|
---|
146 | {
|
---|
147 | foreach (var nf in Field._table.WorkSheet.Workbook.Styles.NumberFormats)
|
---|
148 | {
|
---|
149 | if (nf.NumFmtId == NumFmtId)
|
---|
150 | {
|
---|
151 | return nf.Format;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | return Field._table.WorkSheet.Workbook.Styles.NumberFormats[0].Format;
|
---|
155 | }
|
---|
156 | set
|
---|
157 | {
|
---|
158 | var styles = Field._table.WorkSheet.Workbook.Styles;
|
---|
159 |
|
---|
160 | ExcelNumberFormatXml nf = null;
|
---|
161 | if (!styles.NumberFormats.FindByID(value, ref nf))
|
---|
162 | {
|
---|
163 | nf = new ExcelNumberFormatXml(NameSpaceManager) { Format = value, NumFmtId = styles.NumberFormats.NextId++ };
|
---|
164 | styles.NumberFormats.Add(value, nf);
|
---|
165 | }
|
---|
166 | NumFmtId = nf.NumFmtId;
|
---|
167 | }
|
---|
168 | }
|
---|
169 | /// <summary>
|
---|
170 | /// Type of aggregate function
|
---|
171 | /// </summary>
|
---|
172 | public DataFieldFunctions Function
|
---|
173 | {
|
---|
174 | get
|
---|
175 | {
|
---|
176 | string s=GetXmlNodeString("@subtotal");
|
---|
177 | if(s=="")
|
---|
178 | {
|
---|
179 | return DataFieldFunctions.None;
|
---|
180 | }
|
---|
181 | else
|
---|
182 | {
|
---|
183 | return (DataFieldFunctions)Enum.Parse(typeof(DataFieldFunctions), s, true);
|
---|
184 | }
|
---|
185 | }
|
---|
186 | set
|
---|
187 | {
|
---|
188 | string v;
|
---|
189 | switch(value)
|
---|
190 | {
|
---|
191 | case DataFieldFunctions.None:
|
---|
192 | DeleteNode("@subtotal");
|
---|
193 | return;
|
---|
194 | case DataFieldFunctions.CountNums:
|
---|
195 | v="CountNums";
|
---|
196 | break;
|
---|
197 | case DataFieldFunctions.StdDev:
|
---|
198 | v="stdDev";
|
---|
199 | break;
|
---|
200 | case DataFieldFunctions.StdDevP:
|
---|
201 | v="stdDevP";
|
---|
202 | break;
|
---|
203 | default:
|
---|
204 | v=value.ToString().ToLower(CultureInfo.InvariantCulture);
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | SetXmlNodeString("@subtotal", v);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | /////Since we have no items, Excel will crash when we use showDataAs options that require baseItem's
|
---|
211 | //public eShowDataAs ShowDataAs
|
---|
212 | //{
|
---|
213 | // get
|
---|
214 | // {
|
---|
215 | // string s = GetXmlNodeString("@showDataAs");
|
---|
216 | // if (s == "")
|
---|
217 | // {
|
---|
218 | // return eShowDataAs.Normal;
|
---|
219 | // }
|
---|
220 | // else
|
---|
221 | // {
|
---|
222 | // return (eShowDataAs)Enum.Parse(typeof(eShowDataAs), s, true);
|
---|
223 | // }
|
---|
224 | // }
|
---|
225 | // set
|
---|
226 | // {
|
---|
227 | // string v = value.ToString();
|
---|
228 | // v = v.Substring(0, 1).ToLower() + v.Substring(1);
|
---|
229 | // SetXmlNodeString("@showDataAs", v);
|
---|
230 | // }
|
---|
231 | //}
|
---|
232 | }
|
---|
233 | }
|
---|