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 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using System.Xml;
|
---|
36 |
|
---|
37 | namespace OfficeOpenXml.Table
|
---|
38 | {
|
---|
39 | /// <summary>
|
---|
40 | /// Build-in table row functions
|
---|
41 | /// </summary>
|
---|
42 | public enum RowFunctions
|
---|
43 | {
|
---|
44 | Average,
|
---|
45 | Count,
|
---|
46 | CountNums,
|
---|
47 | Custom,
|
---|
48 | Max,
|
---|
49 | Min,
|
---|
50 | None,
|
---|
51 | StdDev,
|
---|
52 | Sum,
|
---|
53 | Var
|
---|
54 | }
|
---|
55 |
|
---|
56 | /// <summary>
|
---|
57 | /// A table column
|
---|
58 | /// </summary>
|
---|
59 | public class ExcelTableColumn : XmlHelper
|
---|
60 | {
|
---|
61 | ExcelTable _tbl;
|
---|
62 | internal ExcelTableColumn(XmlNamespaceManager ns, XmlNode topNode, ExcelTable tbl, int pos) :
|
---|
63 | base(ns, topNode)
|
---|
64 | {
|
---|
65 | _tbl = tbl;
|
---|
66 | Position = pos;
|
---|
67 | }
|
---|
68 | /// <summary>
|
---|
69 | /// The column id
|
---|
70 | /// </summary>
|
---|
71 | public int Id
|
---|
72 | {
|
---|
73 | get
|
---|
74 | {
|
---|
75 | return GetXmlNodeInt("@id");
|
---|
76 | }
|
---|
77 | set
|
---|
78 | {
|
---|
79 | SetXmlNodeString("@id", value.ToString());
|
---|
80 | }
|
---|
81 | }
|
---|
82 | /// <summary>
|
---|
83 | /// The position of the column
|
---|
84 | /// </summary>
|
---|
85 | public int Position
|
---|
86 | {
|
---|
87 | get;
|
---|
88 | private set;
|
---|
89 | }
|
---|
90 | /// <summary>
|
---|
91 | /// The name of the column
|
---|
92 | /// </summary>
|
---|
93 | public string Name
|
---|
94 | {
|
---|
95 | get
|
---|
96 | {
|
---|
97 | return GetXmlNodeString("@name");
|
---|
98 | }
|
---|
99 | set
|
---|
100 | {
|
---|
101 | SetXmlNodeString("@name", value);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | /// <summary>
|
---|
105 | /// A string text in the total row
|
---|
106 | /// </summary>
|
---|
107 | public string TotalsRowLabel
|
---|
108 | {
|
---|
109 | get
|
---|
110 | {
|
---|
111 | return GetXmlNodeString("@totalsRowLabel");
|
---|
112 | }
|
---|
113 | set
|
---|
114 | {
|
---|
115 | SetXmlNodeString("@totalsRowLabel", value);
|
---|
116 | }
|
---|
117 | }
|
---|
118 | /// <summary>
|
---|
119 | /// Build-in total row functions.
|
---|
120 | /// To set a custom Total row formula use the TotalsRowFormula property
|
---|
121 | /// <seealso cref="TotalsRowFormula"/>
|
---|
122 | /// </summary>
|
---|
123 | public RowFunctions TotalsRowFunction
|
---|
124 | {
|
---|
125 | get
|
---|
126 | {
|
---|
127 | if (GetXmlNodeString("@totalsRowFunction") == "")
|
---|
128 | {
|
---|
129 | return RowFunctions.None;
|
---|
130 | }
|
---|
131 | else
|
---|
132 | {
|
---|
133 | return (RowFunctions)Enum.Parse(typeof(RowFunctions), GetXmlNodeString("@totalsRowFunction"), true);
|
---|
134 | }
|
---|
135 | }
|
---|
136 | set
|
---|
137 | {
|
---|
138 | if (value == RowFunctions.Custom)
|
---|
139 | {
|
---|
140 | throw(new Exception("Use the TotalsRowFormula-property to set a custom table formula"));
|
---|
141 | }
|
---|
142 | string s = value.ToString();
|
---|
143 | s = s.Substring(0, 1).ToLower() + s.Substring(1, s.Length - 1);
|
---|
144 | SetXmlNodeString("@totalsRowFunction", s);
|
---|
145 | }
|
---|
146 | }
|
---|
147 | const string TOTALSROWFORMULA_PATH = "d:totalsRowFormula";
|
---|
148 | /// <summary>
|
---|
149 | /// Sets a custom Totals row Formula.
|
---|
150 | /// Be carefull with this property since it is not validated.
|
---|
151 | /// <example>
|
---|
152 | /// tbl.Columns[9].TotalsRowFormula = string.Format("SUM([{0}])",tbl.Columns[9].Name);
|
---|
153 | /// </example>
|
---|
154 | /// </summary>
|
---|
155 | public string TotalsRowFormula
|
---|
156 | {
|
---|
157 | get
|
---|
158 | {
|
---|
159 | return GetXmlNodeString(TOTALSROWFORMULA_PATH);
|
---|
160 | }
|
---|
161 | set
|
---|
162 | {
|
---|
163 | if (value.StartsWith("=")) value = value.Substring(1, value.Length - 1);
|
---|
164 | SetXmlNodeString("@totalsRowFunction", "custom");
|
---|
165 | SetXmlNodeString(TOTALSROWFORMULA_PATH, value);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | const string DATACELLSTYLE_PATH = "@dataCellStyle";
|
---|
169 | /// <summary>
|
---|
170 | /// The named style for datacells in the column
|
---|
171 | /// </summary>
|
---|
172 | public string DataCellStyleName
|
---|
173 | {
|
---|
174 | get
|
---|
175 | {
|
---|
176 | return GetXmlNodeString(DATACELLSTYLE_PATH);
|
---|
177 | }
|
---|
178 | set
|
---|
179 | {
|
---|
180 | if(_tbl.WorkSheet.Workbook.Styles.NamedStyles.FindIndexByID(value)<0)
|
---|
181 | {
|
---|
182 | throw(new Exception(string.Format("Named style {0} does not exist.",value)));
|
---|
183 | }
|
---|
184 | SetXmlNodeString(TopNode, DATACELLSTYLE_PATH, value,true);
|
---|
185 |
|
---|
186 | int fromRow=_tbl.Address._fromRow + (_tbl.ShowHeader?1:0),
|
---|
187 | toRow=_tbl.Address._toRow - (_tbl.ShowTotal?1:0),
|
---|
188 | col=_tbl.Address._fromCol+Position;
|
---|
189 |
|
---|
190 | if (fromRow < toRow)
|
---|
191 | {
|
---|
192 | _tbl.WorkSheet.Cells[fromRow, col, toRow, col].StyleName = value;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | const string CALCULATEDCOLUMNFORMULA_PATH = "d:calculatedColumnFormula";
|
---|
197 | /// <summary>
|
---|
198 | /// Sets a calculated column Formula.
|
---|
199 | /// Be carefull with this property since it is not validated.
|
---|
200 | /// <example>
|
---|
201 | /// tbl.Columns[9].CalculatedColumnFormula = string.Format("SUM(MyDataTable[[#This Row],[{0}]])",tbl.Columns[9].Name);
|
---|
202 | /// </example>
|
---|
203 | /// </summary>
|
---|
204 | public string CalculatedColumnFormula
|
---|
205 | {
|
---|
206 | get
|
---|
207 | {
|
---|
208 | return GetXmlNodeString(CALCULATEDCOLUMNFORMULA_PATH);
|
---|
209 | }
|
---|
210 | set
|
---|
211 | {
|
---|
212 | if (value.StartsWith("=")) value = value.Substring(1, value.Length - 1);
|
---|
213 | SetXmlNodeString(CALCULATEDCOLUMNFORMULA_PATH, value);
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|
218 | }
|
---|