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 10-SEP-2009
|
---|
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.Globalization;
|
---|
36 | using OfficeOpenXml.Style;
|
---|
37 | using System.Text.RegularExpressions;
|
---|
38 | namespace OfficeOpenXml
|
---|
39 | {
|
---|
40 | internal class ExcelCell : ExcelCellBase, IExcelCell, IRangeID
|
---|
41 | {
|
---|
42 | [Flags]
|
---|
43 | private enum flags
|
---|
44 | {
|
---|
45 | isMerged=1,
|
---|
46 | isRichText=2,
|
---|
47 |
|
---|
48 | }
|
---|
49 | #region Cell Private Properties
|
---|
50 | private ExcelWorksheet _worksheet;
|
---|
51 | private int _row;
|
---|
52 | private int _col;
|
---|
53 | internal string _formula="";
|
---|
54 | internal string _formulaR1C1 = "";
|
---|
55 | private Uri _hyperlink = null;
|
---|
56 | string _dataType = "";
|
---|
57 | #endregion
|
---|
58 | #region ExcelCell Constructor
|
---|
59 | /// <summary>
|
---|
60 | /// A cell in the worksheet.
|
---|
61 | /// </summary>
|
---|
62 | /// <param name="worksheet">A reference to the worksheet</param>
|
---|
63 | /// <param name="row">The row number</param>
|
---|
64 | /// <param name="col">The column number</param>
|
---|
65 | internal ExcelCell(ExcelWorksheet worksheet, int row, int col)
|
---|
66 | {
|
---|
67 | if (row < 1 || col < 1)
|
---|
68 | throw new ArgumentException("Negative row and column numbers are not allowed");
|
---|
69 | if (row > ExcelPackage.MaxRows || col > ExcelPackage.MaxColumns)
|
---|
70 | throw new ArgumentException("Row or column numbers are out of range");
|
---|
71 | if (worksheet == null)
|
---|
72 | throw new ArgumentException("Worksheet must be set to a valid reference");
|
---|
73 |
|
---|
74 | _row = row;
|
---|
75 | _col = col;
|
---|
76 | _worksheet = worksheet;
|
---|
77 | if (col < worksheet._minCol) worksheet._minCol = col;
|
---|
78 | if (col > worksheet._maxCol) worksheet._maxCol = col;
|
---|
79 | _sharedFormulaID = int.MinValue;
|
---|
80 | IsRichText = false;
|
---|
81 | }
|
---|
82 | internal ExcelCell(ExcelWorksheet worksheet, string cellAddress)
|
---|
83 | {
|
---|
84 | _worksheet = worksheet;
|
---|
85 | GetRowColFromAddress(cellAddress, out _row, out _col);
|
---|
86 | if (_col < worksheet._minCol) worksheet._minCol = _col;
|
---|
87 | if (_col > worksheet._maxCol) worksheet._maxCol = _col;
|
---|
88 | _sharedFormulaID = int.MinValue;
|
---|
89 | IsRichText = false;
|
---|
90 | }
|
---|
91 | #endregion
|
---|
92 | internal ulong CellID
|
---|
93 | {
|
---|
94 | get
|
---|
95 | {
|
---|
96 | return GetCellID(_worksheet.SheetID, Row, Column);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | #region ExcelCell Public Properties
|
---|
100 |
|
---|
101 | /// <summary>
|
---|
102 | /// Row number
|
---|
103 | /// </summary>
|
---|
104 | internal int Row { get { return _row; } set { _row = value; } }
|
---|
105 | /// <summary>
|
---|
106 | /// Column number
|
---|
107 | /// </summary>
|
---|
108 | internal int Column { get { return _col; } set { _col = value; } }
|
---|
109 | /// <summary>
|
---|
110 | /// The address
|
---|
111 | /// </summary>
|
---|
112 | internal string CellAddress { get { return GetAddress(_row, _col); } }
|
---|
113 | /// <summary>
|
---|
114 | /// Returns true if the cell's contents are numeric.
|
---|
115 | /// </summary>
|
---|
116 | public bool IsNumeric { get { return (Value is decimal || Value.GetType().IsPrimitive ); } }
|
---|
117 | #region ExcelCell Value
|
---|
118 | internal object _value = null;
|
---|
119 | /// <summary>
|
---|
120 | /// Gets/sets the value of the cell.
|
---|
121 | /// </summary>
|
---|
122 | public object Value
|
---|
123 | {
|
---|
124 | get
|
---|
125 | {
|
---|
126 | return _value;
|
---|
127 | }
|
---|
128 | set
|
---|
129 | {
|
---|
130 | SetValueRichText(value);
|
---|
131 | if (IsRichText) IsRichText = false;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | internal void SetValueRichText(object value)
|
---|
135 | {
|
---|
136 | _value = value;
|
---|
137 | if (value is string) DataType = "s"; else DataType = "";
|
---|
138 | Formula = "";
|
---|
139 | }
|
---|
140 | /// <summary>
|
---|
141 | /// If cell has inline formatting.
|
---|
142 | /// </summary>
|
---|
143 | public bool IsRichText { get; set; }
|
---|
144 | /// <summary>
|
---|
145 | /// If the cell is merged with other cells
|
---|
146 | /// </summary>
|
---|
147 | public bool Merge { get; internal set; }
|
---|
148 | /// <summary>
|
---|
149 | /// Merge Id
|
---|
150 | /// </summary>
|
---|
151 | internal int MergeId { get; set; }
|
---|
152 | #endregion
|
---|
153 |
|
---|
154 | #region ExcelCell DataType
|
---|
155 | /// <summary>
|
---|
156 | /// Datatype
|
---|
157 | /// TODO: remove
|
---|
158 | /// </summary>
|
---|
159 | internal string DataType
|
---|
160 | {
|
---|
161 | get
|
---|
162 | {
|
---|
163 | return (_dataType);
|
---|
164 | }
|
---|
165 | set
|
---|
166 | {
|
---|
167 | _dataType = value;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | #endregion
|
---|
171 |
|
---|
172 | #region ExcelCell Style
|
---|
173 | string _styleName=null;
|
---|
174 | /// <summary>
|
---|
175 | /// Optional named style for the cell
|
---|
176 | /// </summary>
|
---|
177 | public string StyleName
|
---|
178 | {
|
---|
179 | get
|
---|
180 | {
|
---|
181 | if (_styleName == null)
|
---|
182 | {
|
---|
183 | foreach (var ns in _worksheet.Workbook.Styles.NamedStyles)
|
---|
184 | {
|
---|
185 | if (ns.StyleXfId == StyleID)
|
---|
186 | {
|
---|
187 | _styleName = ns.Name;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | if (_styleName == null)
|
---|
192 | {
|
---|
193 | _styleName = "Normal";
|
---|
194 | }
|
---|
195 | }
|
---|
196 | return _styleName;
|
---|
197 | }
|
---|
198 | set
|
---|
199 | {
|
---|
200 | _styleID = _worksheet.Workbook.Styles.GetStyleIdFromName(value);
|
---|
201 | _styleName = value;
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | int _styleID=0;
|
---|
206 | /// <summary>
|
---|
207 | /// The style ID for the cell. Reference to the style collection
|
---|
208 | /// </summary>
|
---|
209 | public int StyleID
|
---|
210 | {
|
---|
211 | get
|
---|
212 | {
|
---|
213 | if(_styleID>0)
|
---|
214 | return _styleID;
|
---|
215 | else if (_worksheet._rows != null && _worksheet._rows.ContainsKey(ExcelRow.GetRowID(_worksheet.SheetID, Row)) && _worksheet.Row(Row).StyleID>0)
|
---|
216 | {
|
---|
217 | return _worksheet.Row(Row).StyleID;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | {
|
---|
221 | ExcelColumn col = GetColumn(Column);
|
---|
222 | if(col==null)
|
---|
223 | {
|
---|
224 | return 0;
|
---|
225 | }
|
---|
226 | else
|
---|
227 | {
|
---|
228 | return col.StyleID;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | set
|
---|
233 | {
|
---|
234 | _styleID = value;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | private ExcelColumn GetColumn(int col)
|
---|
239 | {
|
---|
240 | foreach (ExcelColumn column in _worksheet._columns)
|
---|
241 | {
|
---|
242 | if (col >= column.ColumnMin && col <= column.ColumnMax)
|
---|
243 | {
|
---|
244 | return column;
|
---|
245 | }
|
---|
246 | }
|
---|
247 | return null;
|
---|
248 | }
|
---|
249 | internal int GetCellStyleID()
|
---|
250 | {
|
---|
251 | return _styleID;
|
---|
252 | }
|
---|
253 | public ExcelStyle Style
|
---|
254 | {
|
---|
255 | get
|
---|
256 | {
|
---|
257 | return _worksheet.Workbook.Styles.GetStyleObject(StyleID, _worksheet.PositionID, CellAddress);
|
---|
258 | }
|
---|
259 | }
|
---|
260 | internal void SetNewStyleName(string Name, int Id)
|
---|
261 | {
|
---|
262 | _styleID = Id;
|
---|
263 | _styleName = Name;
|
---|
264 |
|
---|
265 | }
|
---|
266 | #endregion
|
---|
267 |
|
---|
268 | #region ExcelCell Hyperlink
|
---|
269 | /// <summary>
|
---|
270 | /// The cells cell's Hyperlink
|
---|
271 | /// </summary>
|
---|
272 | public Uri Hyperlink
|
---|
273 | {
|
---|
274 | get
|
---|
275 | {
|
---|
276 | return (_hyperlink);
|
---|
277 | }
|
---|
278 | set
|
---|
279 | {
|
---|
280 | _hyperlink = value;
|
---|
281 | if ((Value == null || Value.ToString() == ""))
|
---|
282 | {
|
---|
283 | if (value is ExcelHyperLink)
|
---|
284 | {
|
---|
285 | Value = (value as ExcelHyperLink).Display;
|
---|
286 | }
|
---|
287 | else
|
---|
288 | {
|
---|
289 | Value = _hyperlink.AbsoluteUri;
|
---|
290 | }
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 | internal string HyperLinkRId
|
---|
295 | {
|
---|
296 | get;
|
---|
297 | set;
|
---|
298 | }
|
---|
299 | #endregion
|
---|
300 |
|
---|
301 | #region ExcelCell Formula
|
---|
302 | /// <summary>
|
---|
303 | /// The cell's formula.
|
---|
304 | /// </summary>
|
---|
305 | public string Formula
|
---|
306 | {
|
---|
307 | get
|
---|
308 | {
|
---|
309 | if (SharedFormulaID < 0)
|
---|
310 | {
|
---|
311 | if (_formula == "")
|
---|
312 | {
|
---|
313 | return (TranslateFromR1C1(_formulaR1C1, Row, Column));
|
---|
314 | }
|
---|
315 | else
|
---|
316 | {
|
---|
317 | return (_formula);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | else
|
---|
321 | {
|
---|
322 | if (_worksheet._sharedFormulas.ContainsKey(SharedFormulaID))
|
---|
323 | {
|
---|
324 | var f = _worksheet._sharedFormulas[SharedFormulaID];
|
---|
325 | if (f.StartRow == Row && f.StartCol == Column)
|
---|
326 | {
|
---|
327 | return f.Formula;
|
---|
328 | }
|
---|
329 | else
|
---|
330 | {
|
---|
331 | return TranslateFromR1C1(TranslateToR1C1(f.Formula, f.StartRow, f.StartCol), Row, Column);
|
---|
332 | }
|
---|
333 |
|
---|
334 | }
|
---|
335 | else
|
---|
336 | {
|
---|
337 | throw(new Exception("Shared formula reference (SI) is invalid"));
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }
|
---|
341 | set
|
---|
342 | {
|
---|
343 | _formula = value;
|
---|
344 | _formulaR1C1 = "";
|
---|
345 | _sharedFormulaID = int.MinValue;
|
---|
346 | if (_formula!="" && !_worksheet._formulaCells.ContainsKey(CellID))
|
---|
347 | {
|
---|
348 | _worksheet._formulaCells.Add(this);
|
---|
349 | }
|
---|
350 | }
|
---|
351 | }
|
---|
352 | /// <summary>
|
---|
353 | /// The cell's formula using R1C1 style.
|
---|
354 | /// </summary>
|
---|
355 | public string FormulaR1C1
|
---|
356 | {
|
---|
357 | get
|
---|
358 | {
|
---|
359 | if (SharedFormulaID < 0)
|
---|
360 | {
|
---|
361 | if (_formulaR1C1 == "")
|
---|
362 | {
|
---|
363 | return TranslateToR1C1(_formula, Row, Column);
|
---|
364 | }
|
---|
365 | else
|
---|
366 | {
|
---|
367 | return (_formulaR1C1);
|
---|
368 | }
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | if (_worksheet._sharedFormulas.ContainsKey(SharedFormulaID))
|
---|
373 | {
|
---|
374 | var f = _worksheet._sharedFormulas[SharedFormulaID];
|
---|
375 | return TranslateToR1C1(f.Formula, f.StartRow, f.StartCol);
|
---|
376 | }
|
---|
377 | else
|
---|
378 | {
|
---|
379 | throw (new Exception("Shared formula reference (SI) is invalid"));
|
---|
380 | }
|
---|
381 | }
|
---|
382 | }
|
---|
383 | set
|
---|
384 | {
|
---|
385 | // Example cell content for formulas
|
---|
386 | // <f>RC1</f>
|
---|
387 | // <f>SUM(RC1:RC3)</f>
|
---|
388 | // <f>R[-1]C[-2]+R[-1]C[-1]</f>
|
---|
389 | _formulaR1C1 = value;
|
---|
390 | _formula = "";
|
---|
391 | SharedFormulaID = int.MinValue;
|
---|
392 | if (!_worksheet._formulaCells.ContainsKey(CellID))
|
---|
393 | {
|
---|
394 | _worksheet._formulaCells.Add(this);
|
---|
395 | }
|
---|
396 | }
|
---|
397 | }
|
---|
398 | internal int _sharedFormulaID;
|
---|
399 | /// <summary>
|
---|
400 | /// Id for the shared formula
|
---|
401 | /// </summary>
|
---|
402 | internal int SharedFormulaID {
|
---|
403 | get
|
---|
404 | {
|
---|
405 | return _sharedFormulaID;
|
---|
406 | }
|
---|
407 | set
|
---|
408 | {
|
---|
409 | _sharedFormulaID = value;
|
---|
410 | if(_worksheet._formulaCells.ContainsKey(CellID)) _worksheet._formulaCells.Delete(CellID);
|
---|
411 | }
|
---|
412 | }
|
---|
413 | public bool IsArrayFormula { get; internal set; }
|
---|
414 | #endregion
|
---|
415 | /// <summary>
|
---|
416 | /// Returns the cell's value as a string.
|
---|
417 | /// </summary>
|
---|
418 | /// <returns>The cell's value</returns>
|
---|
419 | public override string ToString() { return Value.ToString(); }
|
---|
420 | #endregion
|
---|
421 | #region ExcelCell Private Methods
|
---|
422 | #endregion
|
---|
423 | #region IRangeID Members
|
---|
424 |
|
---|
425 | ulong IRangeID.RangeID
|
---|
426 | {
|
---|
427 | get
|
---|
428 | {
|
---|
429 | return GetCellID(_worksheet.SheetID, Row, Column);
|
---|
430 | }
|
---|
431 | set
|
---|
432 | {
|
---|
433 | _col = ((int)(value >> 15) & 0x3FF);
|
---|
434 | _row = ((int)(value >> 29));
|
---|
435 | }
|
---|
436 | }
|
---|
437 |
|
---|
438 | #endregion
|
---|
439 | internal ExcelCell Clone(ExcelWorksheet added)
|
---|
440 | {
|
---|
441 | return Clone(added, _row, _col);
|
---|
442 | }
|
---|
443 | internal ExcelCell Clone(ExcelWorksheet added, int row, int col)
|
---|
444 | {
|
---|
445 | ExcelCell newCell = new ExcelCell(added, row, col);
|
---|
446 | if(_hyperlink!=null) newCell.Hyperlink = Hyperlink;
|
---|
447 | newCell._formula = _formula;
|
---|
448 | newCell._formulaR1C1 = _formulaR1C1;
|
---|
449 | newCell.IsRichText = IsRichText;
|
---|
450 | newCell.Merge = Merge;
|
---|
451 | newCell._sharedFormulaID = _sharedFormulaID;
|
---|
452 | newCell._styleName = _styleName;
|
---|
453 | newCell._styleID = _styleID;
|
---|
454 | newCell._value = _value;
|
---|
455 | return newCell;
|
---|
456 | }
|
---|
457 | }
|
---|
458 | }
|
---|