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 Initial Release 2009-10-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-27
|
---|
31 | *******************************************************************************/
|
---|
32 |
|
---|
33 | using System;
|
---|
34 | using System.Xml;
|
---|
35 | using OfficeOpenXml.Style;
|
---|
36 | namespace OfficeOpenXml
|
---|
37 | {
|
---|
38 | internal class RowInternal
|
---|
39 | {
|
---|
40 | internal double Height=-1;
|
---|
41 | internal bool Hidden;
|
---|
42 | internal bool Collapsed;
|
---|
43 | internal short OutlineLevel;
|
---|
44 | internal bool PageBreak;
|
---|
45 | internal bool Phonetic;
|
---|
46 | internal bool CustomHeight;
|
---|
47 | internal int MergeID;
|
---|
48 | internal RowInternal Clone()
|
---|
49 | {
|
---|
50 | return new RowInternal()
|
---|
51 | {
|
---|
52 | Height=Height,
|
---|
53 | Hidden=Hidden,
|
---|
54 | Collapsed=Collapsed,
|
---|
55 | OutlineLevel=OutlineLevel,
|
---|
56 | PageBreak=PageBreak,
|
---|
57 | Phonetic=Phonetic,
|
---|
58 | CustomHeight=CustomHeight,
|
---|
59 | MergeID=MergeID
|
---|
60 | };
|
---|
61 | }
|
---|
62 | }
|
---|
63 | /// <summary>
|
---|
64 | /// Represents an individual row in the spreadsheet.
|
---|
65 | /// </summary>
|
---|
66 | public class ExcelRow : IRangeID
|
---|
67 | {
|
---|
68 | private ExcelWorksheet _worksheet;
|
---|
69 | private XmlElement _rowElement = null;
|
---|
70 | /// <summary>
|
---|
71 | /// Internal RowID.
|
---|
72 | /// </summary>
|
---|
73 | [Obsolete]
|
---|
74 | public ulong RowID
|
---|
75 | {
|
---|
76 | get
|
---|
77 | {
|
---|
78 | return GetRowID(_worksheet.SheetID, Row);
|
---|
79 | }
|
---|
80 | }
|
---|
81 | #region ExcelRow Constructor
|
---|
82 | /// <summary>
|
---|
83 | /// Creates a new instance of the ExcelRow class.
|
---|
84 | /// For internal use only!
|
---|
85 | /// </summary>
|
---|
86 | /// <param name="Worksheet">The parent worksheet</param>
|
---|
87 | /// <param name="row">The row number</param>
|
---|
88 | internal ExcelRow(ExcelWorksheet Worksheet, int row)
|
---|
89 | {
|
---|
90 | _worksheet = Worksheet;
|
---|
91 | Row = row;
|
---|
92 | }
|
---|
93 | #endregion
|
---|
94 |
|
---|
95 | /// <summary>
|
---|
96 | /// Provides access to the node representing the row.
|
---|
97 | /// </summary>
|
---|
98 | internal XmlNode Node { get { return (_rowElement); } }
|
---|
99 |
|
---|
100 | #region ExcelRow Hidden
|
---|
101 | /// <summary>
|
---|
102 | /// Allows the row to be hidden in the worksheet
|
---|
103 | /// </summary>
|
---|
104 | public bool Hidden
|
---|
105 | {
|
---|
106 | get
|
---|
107 | {
|
---|
108 | var r=(RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
109 | if (r == null)
|
---|
110 | {
|
---|
111 | return false;
|
---|
112 | }
|
---|
113 | else
|
---|
114 | {
|
---|
115 | return r.Hidden;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | set
|
---|
119 | {
|
---|
120 | var r = GetRowInternal();
|
---|
121 | r.Hidden=value;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | #endregion
|
---|
125 |
|
---|
126 | #region ExcelRow Height
|
---|
127 | /// <summary>
|
---|
128 | /// Sets the height of the row
|
---|
129 | /// </summary>
|
---|
130 | public double Height
|
---|
131 | {
|
---|
132 | get
|
---|
133 | {
|
---|
134 | var r = (RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
135 | if (r == null || r.Height<0)
|
---|
136 | {
|
---|
137 | return _worksheet.DefaultRowHeight;
|
---|
138 | }
|
---|
139 | else
|
---|
140 | {
|
---|
141 | return r.Height;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | set
|
---|
145 | {
|
---|
146 | var r = GetRowInternal();
|
---|
147 | if (_worksheet._package.DoAdjustDrawings)
|
---|
148 | {
|
---|
149 | var pos = _worksheet.Drawings.GetDrawingWidths();
|
---|
150 | r.Height = value;
|
---|
151 | _worksheet.Drawings.AdjustHeight(pos);
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | r.Height = value;
|
---|
156 | }
|
---|
157 |
|
---|
158 | if (r.Hidden && value != 0)
|
---|
159 | {
|
---|
160 | Hidden = false;
|
---|
161 | }
|
---|
162 | r.CustomHeight = (value != _worksheet.DefaultRowHeight);
|
---|
163 | }
|
---|
164 | }
|
---|
165 | /// <summary>
|
---|
166 | /// Set to true if You don't want the row to Autosize
|
---|
167 | /// </summary>
|
---|
168 | public bool CustomHeight
|
---|
169 | {
|
---|
170 | get
|
---|
171 | {
|
---|
172 | var r = (RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
173 | if (r == null)
|
---|
174 | {
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 | else
|
---|
178 | {
|
---|
179 | return r.CustomHeight;
|
---|
180 | }
|
---|
181 | }
|
---|
182 | set
|
---|
183 | {
|
---|
184 | var r = GetRowInternal();
|
---|
185 | r.CustomHeight = value;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | #endregion
|
---|
189 |
|
---|
190 | internal string _styleName = "";
|
---|
191 | /// <summary>
|
---|
192 | /// Sets the style for the entire column using a style name.
|
---|
193 | /// </summary>
|
---|
194 | public string StyleName
|
---|
195 | {
|
---|
196 | get
|
---|
197 | {
|
---|
198 | return _styleName;
|
---|
199 | }
|
---|
200 | set
|
---|
201 | {
|
---|
202 | StyleID = _worksheet.Workbook.Styles.GetStyleIdFromName(value);
|
---|
203 | _styleName = value;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | /// <summary>
|
---|
207 | /// Sets the style for the entire row using the style ID.
|
---|
208 | /// </summary>
|
---|
209 | public int StyleID
|
---|
210 | {
|
---|
211 | get
|
---|
212 | {
|
---|
213 | return _worksheet._styles.GetValue(Row, 0);
|
---|
214 | }
|
---|
215 | set
|
---|
216 | {
|
---|
217 | _worksheet._styles.SetValue(Row, 0, value);
|
---|
218 | }
|
---|
219 | }
|
---|
220 |
|
---|
221 | /// <summary>
|
---|
222 | /// Rownumber
|
---|
223 | /// </summary>
|
---|
224 | public int Row
|
---|
225 | {
|
---|
226 | get;
|
---|
227 | set;
|
---|
228 | }
|
---|
229 | /// <summary>
|
---|
230 | /// If outline level is set this tells that the row is collapsed
|
---|
231 | /// </summary>
|
---|
232 | public bool Collapsed
|
---|
233 | {
|
---|
234 | get
|
---|
235 | {
|
---|
236 | var r=(RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
237 | if (r == null)
|
---|
238 | {
|
---|
239 | return false;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | {
|
---|
243 | return r.Collapsed;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | set
|
---|
247 | {
|
---|
248 | var r = GetRowInternal();
|
---|
249 | r.Collapsed = value;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | /// <summary>
|
---|
253 | /// Outline level.
|
---|
254 | /// </summary>
|
---|
255 | public int OutlineLevel
|
---|
256 | {
|
---|
257 | get
|
---|
258 | {
|
---|
259 | var r=(RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
260 | if (r == null)
|
---|
261 | {
|
---|
262 | return 0;
|
---|
263 | }
|
---|
264 | else
|
---|
265 | {
|
---|
266 | return r.OutlineLevel;
|
---|
267 | }
|
---|
268 | }
|
---|
269 | set
|
---|
270 | {
|
---|
271 | var r = GetRowInternal();
|
---|
272 | r.OutlineLevel=(short)value;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | private RowInternal GetRowInternal()
|
---|
277 | {
|
---|
278 | var r = (RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
279 | if (r == null)
|
---|
280 | {
|
---|
281 | r = new RowInternal();
|
---|
282 | _worksheet._values.SetValue(Row, 0, r);
|
---|
283 | }
|
---|
284 | return r;
|
---|
285 | }
|
---|
286 | /// <summary>
|
---|
287 | /// Show phonetic Information
|
---|
288 | /// </summary>
|
---|
289 | public bool Phonetic
|
---|
290 | {
|
---|
291 | get
|
---|
292 | {
|
---|
293 | var r = (RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
294 | if (r == null)
|
---|
295 | {
|
---|
296 | return false;
|
---|
297 | }
|
---|
298 | else
|
---|
299 | {
|
---|
300 | return r.Phonetic;
|
---|
301 | }
|
---|
302 | }
|
---|
303 | set
|
---|
304 | {
|
---|
305 | var r = GetRowInternal();
|
---|
306 | r.Phonetic = value;
|
---|
307 | }
|
---|
308 | }
|
---|
309 | /// <summary>
|
---|
310 | /// The Style applied to the whole row. Only effekt cells with no individual style set.
|
---|
311 | /// Use ExcelRange object if you want to set specific styles.
|
---|
312 | /// </summary>
|
---|
313 | public ExcelStyle Style
|
---|
314 | {
|
---|
315 | get
|
---|
316 | {
|
---|
317 | return _worksheet.Workbook.Styles.GetStyleObject(StyleID,_worksheet.PositionID ,Row.ToString()+":"+Row.ToString());
|
---|
318 | }
|
---|
319 | }
|
---|
320 | /// <summary>
|
---|
321 | /// Adds a manual page break after the row.
|
---|
322 | /// </summary>
|
---|
323 | public bool PageBreak
|
---|
324 | {
|
---|
325 | get
|
---|
326 | {
|
---|
327 | var r = (RowInternal)_worksheet._values.GetValue(Row, 0);
|
---|
328 | if (r == null)
|
---|
329 | {
|
---|
330 | return false;
|
---|
331 | }
|
---|
332 | else
|
---|
333 | {
|
---|
334 | return r.PageBreak;
|
---|
335 | }
|
---|
336 | }
|
---|
337 | set
|
---|
338 | {
|
---|
339 | var r = GetRowInternal();
|
---|
340 | r.PageBreak = value;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | public bool Merged
|
---|
344 | {
|
---|
345 | get
|
---|
346 | {
|
---|
347 | return _worksheet.MergedCells[Row, 0] != null;
|
---|
348 | }
|
---|
349 | set
|
---|
350 | {
|
---|
351 | _worksheet.MergedCells.Add(new ExcelAddressBase(Row, 1, Row, ExcelPackage.MaxColumns), true);
|
---|
352 | }
|
---|
353 | }
|
---|
354 | internal static ulong GetRowID(int sheetID, int row)
|
---|
355 | {
|
---|
356 | return ((ulong)sheetID) + (((ulong)row) << 29);
|
---|
357 |
|
---|
358 | }
|
---|
359 |
|
---|
360 | #region IRangeID Members
|
---|
361 |
|
---|
362 | [Obsolete]
|
---|
363 | ulong IRangeID.RangeID
|
---|
364 | {
|
---|
365 | get
|
---|
366 | {
|
---|
367 | return RowID;
|
---|
368 | }
|
---|
369 | set
|
---|
370 | {
|
---|
371 | Row = ((int)(value >> 29));
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | #endregion
|
---|
376 | /// <summary>
|
---|
377 | /// Copies the current row to a new worksheet
|
---|
378 | /// </summary>
|
---|
379 | /// <param name="added">The worksheet where the copy will be created</param>
|
---|
380 | internal void Clone(ExcelWorksheet added)
|
---|
381 | {
|
---|
382 | ExcelRow newRow = added.Row(Row);
|
---|
383 | newRow.Collapsed = Collapsed;
|
---|
384 | newRow.Height = Height;
|
---|
385 | newRow.Hidden = Hidden;
|
---|
386 | newRow.OutlineLevel = OutlineLevel;
|
---|
387 | newRow.PageBreak = PageBreak;
|
---|
388 | newRow.Phonetic = Phonetic;
|
---|
389 | newRow._styleName = _styleName;
|
---|
390 | newRow.StyleID = StyleID;
|
---|
391 | }
|
---|
392 | }
|
---|
393 | }
|
---|