NanoXLSX.Core 3.2.1
Loading...
Searching...
No Matches
Cell.cs
1/*
2 * NanoXLSX is a small .NET library to generate and read XLSX (Microsoft Excel 2007 or newer) files in an easy and native way
3 * Copyright Raphael Stoeckli © 2026
4 * This library is licensed under the MIT License.
5 * You find a copy of the license in project folder or on: http://opensource.org/licenses/MIT
6 */
7
8using System;
9using System.Collections.Generic;
10using System.Globalization;
11using System.Text;
12using NanoXLSX.Enums;
15using NanoXLSX.Styles;
16using NanoXLSX.Utils;
18
19namespace NanoXLSX
20{
24 public class Cell : IComparable<Cell>
25 {
26 #region constants
27 private const int ASCII_OFFSET = 64;
28 #endregion
29
30 #region enums
34 public enum CellType
35 {
37#pragma warning disable CA1720 // Suppress warning: Identifier contains type name
39#pragma warning restore CA1720
61 }
62
77
92
93 #endregion
94
95 #region privateFileds
96 private Style cellStyle;
97 private int columnNumber;
98 private CellType dataType;
99 private FormulaData formula;
100 private int rowNumber;
101 private object value;
102 private FeatureSet worksheetFeatures;
103 #endregion
104
105 #region properties
106
110 public string CellAddress
111 {
113 set
114 {
115 AddressType addressType;
116 ResolveCellCoordinate(value, out columnNumber, out rowNumber, out addressType);
117 CellAddressType = addressType;
118 }
119 }
120
123 {
124 get { return new Address(ColumnNumber, RowNumber, CellAddressType); }
125 set
126 {
127 ColumnNumber = value.Column;
128 RowNumber = value.Row;
129 CellAddressType = value.Type;
130 }
131 }
132
137 {
138 get { return cellStyle; }
139 }
140
143 public int ColumnNumber
144 {
145 get { return columnNumber; }
146 set
147 {
149 columnNumber = value;
150 }
151 }
152
158 {
159 get { return dataType; }
160 set
161 {
162 if (dataType == value)
163 {
164 return;
165 }
166 if (value == CellType.Formula)
167 {
168 dataType = value;
169 if (formula == null)
170 {
171 Formula = new FormulaData(GetValueAsFormulaExpression());
172 }
173 else
174 {
175 AttachFormulaFeatures();
176 SynchronitzeValueFromFormula();
177 }
178 return;
179 }
180 if (dataType == CellType.Formula)
181 {
182 ClearFormula();
183 }
184 dataType = value;
185 }
186 }
187
190 public int RowNumber
191 {
192 get { return rowNumber; }
193 set
194 {
195 ValidateRowNumber(value);
196 rowNumber = value;
197 }
198 }
199
204 public AddressType CellAddressType { get; set; }
205
211 public object Value
212 {
213 get => this.value;
214 set
215 {
216 this.value = value;
218 if (dataType != CellType.Formula || formula == null)
219 {
220 return;
221 }
222 if (formula.MasterCellAddress == null)
223 {
224 string expression = GetValueAsFormulaExpression();
225 if (!string.Equals(formula.Expression, expression, StringComparison.Ordinal) && formula.DefinedNameReference != null)
226 {
227 formula.DefinedNameReference = null; // Remove additional references
228 }
229 formula.Expression = expression;
230 }
231 }
232
233 }
234
242 {
243 get { return formula; }
244 internal set
245 {
246 if (ReferenceEquals(formula, value))
247 {
248 return;
249 }
250 DetachFormulaFeatures();
251 formula = value;
252 SynchronitzeValueFromFormula();
253 AttachFormulaFeatures();
254 }
255 }
256
257 #endregion
258
259 #region constructors
261 public Cell()
262 {
263 DataType = CellType.Default;
264 }
265
272 public Cell(object value, CellType type)
273 {
274 if (type == CellType.Empty)
275 {
276 this.value = null;
277 }
278 else
279 {
280 this.value = value;
281 }
282 DataType = type;
283 if (type == CellType.Default)
284 {
286 }
287 }
288
296 public Cell(object value, CellType type, string address)
297 {
298 if (type == CellType.Empty)
299 {
300 this.value = null;
301 }
302 else
303 {
304 this.value = value;
305 }
306 DataType = type;
307 CellAddress = address;
308 if (type == CellType.Default)
309 {
311 }
312 }
313
321 public Cell(object value, CellType type, Address address)
322 {
323 if (type == CellType.Empty)
324 {
325 this.value = null;
326 }
327 else
328 {
329 this.value = value;
330 }
331 DataType = type;
332 columnNumber = address.Column;
333 rowNumber = address.Row;
334 CellAddressType = address.Type;
335 if (type == CellType.Default)
336 {
338 }
339 }
340
348 public Cell(object value, CellType type, int column, int row) : this(value, type)
349 {
350 ColumnNumber = column;
351 RowNumber = row;
353 if (type == CellType.Default)
354 {
355 ResolveCellType();
356 }
357 }
358 #endregion
359
360 #region methods
369 public int CompareTo(Cell other)
370 {
371 if (other == null)
372 {
373 return -1;
374 }
375 if (RowNumber == other.RowNumber)
376 {
377 return ColumnNumber.CompareTo(other.ColumnNumber);
378 }
379
380 return RowNumber.CompareTo(other.RowNumber);
381 }
382
388 public override bool Equals(object obj)
389 {
390 if (obj == null || obj.GetType() != typeof(Cell))
391 {
392 return false;
393 }
394 Cell other = (Cell)obj;
395 if (!this.CellAddress2.Equals(other.CellAddress2))
396 {
397 return false;
398 }
399 if (this.cellStyle != null && other.CellStyle != null && !this.CellStyle.Equals(other.CellStyle))
400 {
401 return false;
402 }
403 if (this.Formula != null && other.Formula != null && !this.Formula.Equals(other.Formula))
404 {
405 return false;
406 }
407 if (this.DataType != other.DataType)
408 {
409 return false;
410 }
411 if (this.Value != null && other.Value != null && !this.Value.Equals(other.Value))
412 {
413 return false;
414 }
415 return true;
416 }
417
421 public void RemoveStyle()
422 {
423 cellStyle = null;
424 }
425
426
435 internal Range? SetReference(DefinedName definedName, object cachedValue = null)
436 {
437 if (definedName == null)
438 {
439 throw new WorksheetException("The defined name to set as cell reference must not be null.");
440 }
441 FormulaData formula = this.formula ?? new FormulaData();
442 Range? referenceRange = null;
443 formula.DefinedNameReference = definedName;
444 formula.Expression = definedName.Name;
445 if (definedName.Type == DefinedName.NameType.Range)
446 {
447 formula.Type = FormulaData.FormulaType.Array;
448 referenceRange = TransposeDefinedNameArrayRange(definedName.TextValue);
449 }
450 if (definedName.Type == DefinedName.NameType.Constant)
451 {
452 formula.CachedValue = definedName.TextValue;
453 formula.CachedValueType = FormulaData.ResolveCachedValueType(definedName.Value);
454 }
455 else
456 {
457 if (cachedValue == null || (cachedValue is string stringValue && stringValue.Length == 0))
458 {
459 formula.CachedValueType = CellType.Number;
460 }
461 else
462 {
463 formula.CachedValueType = FormulaData.ResolveCachedValueType(cachedValue);
464 }
465 formula.CachedValue = ParserUtils.ToCachedValueString(cachedValue); // Force value as plain OOXML string
466 }
467 this.DataType = CellType.Formula; // Force type
468 this.Formula = formula;
469 this.value = definedName.Name;
470 return referenceRange;
471 }
472
477 public void ResolveCellType()
478 {
479 if (this.value == null)
480 {
481 DataType = CellType.Empty;
482 this.value = null;
483 return;
484 }
485 if (DataType == CellType.Formula)
486 { return; } // Do not overwrite type
487 Type t = this.value.GetType();
488 if (t == typeof(bool))
489 { DataType = CellType.Bool; }
490 else if (t == typeof(byte) || t == typeof(sbyte))
491 { DataType = CellType.Number; }
492 else if (t == typeof(decimal))
493 { DataType = CellType.Number; }
494 else if (t == typeof(double))
495 { DataType = CellType.Number; }
496 else if (t == typeof(float))
497 { DataType = CellType.Number; }
498 else if (t == typeof(int) || t == typeof(uint))
499 { DataType = CellType.Number; }
500 else if (t == typeof(long) || t == typeof(ulong))
501 { DataType = CellType.Number; }
502 else if (t == typeof(short) || t == typeof(ushort))
503 { DataType = CellType.Number; }
504 else if (t == typeof(DateTime)) // Not native but standard
505 {
506 DataType = CellType.Date;
508 }
509
510 else if (t == typeof(TimeSpan)) // Not native but standard
511 {
512 DataType = CellType.Time;
514 }
515 else if (t == typeof(Errors.FormulaError))
516 { DataType = CellType.Error; }
517 else { DataType = CellType.String; } // Default (char, string, object)
518 }
519
527 public void SetCellLockedState(bool isLocked, bool isHidden)
528 {
529 Style lockStyle;
530 if (cellStyle == null)
531 {
532 lockStyle = new Style();
533 }
534 else
535 {
536 lockStyle = cellStyle.CopyStyle();
537 }
538 lockStyle.CurrentCellXf.Locked = isLocked;
539 lockStyle.CurrentCellXf.Hidden = isHidden;
540 SetStyle(lockStyle);
541 }
542
549 public Style SetStyle(Style style, bool unmanaged = false)
550 {
551 if (style == null)
552 {
553 throw new StyleException("No style to assign was defined");
554 }
555 cellStyle = unmanaged ? style : StyleRepository.Instance.AddStyle(style);
556 return cellStyle;
557 }
558
563 internal Cell Copy()
564 {
565 Cell copy = new Cell
566 {
567 value = this.value,
568 dataType = this.dataType,
569 CellAddress = this.CellAddress,
570 CellAddressType = this.CellAddressType,
571 formula = this.formula?.Copy()
572 };
573 if (this.cellStyle != null)
574 {
575 copy.SetStyle(this.cellStyle, true);
576 }
577 return copy;
578 }
583 public override int GetHashCode()
584 {
585 unchecked
586 {
587 int hash = 17;
588 hash = hash * 31 + columnNumber.GetHashCode();
589 hash = hash * 31 + rowNumber.GetHashCode();
590 hash = hash * 31 + CellAddressType.GetHashCode();
591 hash = hash * 31 + DataType.GetHashCode();
592 hash = hash * 31 + (cellStyle?.GetHashCode() ?? 0);
593 hash = hash * 31 + (value?.GetHashCode() ?? 0);
594 hash = hash * 31 + (Formula?.GetHashCode() ?? 0);
595 return hash;
596 }
597 }
598
605 public static bool operator ==(Cell left, Cell right)
606 {
607 if (ReferenceEquals(left, null))
608 {
609 return ReferenceEquals(right, null);
610 }
611
612 return left.Equals(right);
613 }
614
621 public static bool operator !=(Cell left, Cell right)
622 {
623 return !(left == right);
624 }
625
635 public static bool operator <(Cell left, Cell right)
636 {
637 return ReferenceEquals(left, null) ? !ReferenceEquals(right, null) : left.CompareTo(right) < 0;
638 }
639
649 public static bool operator <=(Cell left, Cell right)
650 {
651 return ReferenceEquals(left, null) || left.CompareTo(right) <= 0;
652 }
653
663 public static bool operator >(Cell left, Cell right)
664 {
665 return !ReferenceEquals(left, null) && left.CompareTo(right) > 0;
666 }
667
677 public static bool operator >=(Cell left, Cell right)
678 {
679 return ReferenceEquals(left, null) ? ReferenceEquals(right, null) : left.CompareTo(right) >= 0;
680 }
681
682 #endregion
683
684 #region staticMethods
691 public static IEnumerable<Cell> ConvertArray<T>(IEnumerable<T> list)
692 {
693 List<Cell> output = new List<Cell>();
694 if (list == null)
695 {
696 return output;
697 }
698 Cell c;
699 object o;
700 Type t;
701 foreach (T item in list)
702 {
703 if (item == null) // DO NOT LISTEN to code suggestions! This is wrong for bool: if (object.Equals(item, default(T)))
704 {
705 c = new Cell(null, CellType.Empty);
706 output.Add(c);
707 continue;
708 }
709 o = item; // intermediate object is necessary to cast the types below
710 t = item.GetType();
711 if (t == typeof(Cell))
712 { c = item as Cell; }
713 else if (t == typeof(bool))
714 { c = new Cell((bool)o, CellType.Bool); }
715 else if (t == typeof(byte))
716 { c = new Cell((byte)o, CellType.Number); }
717 else if (t == typeof(sbyte))
718 { c = new Cell((sbyte)o, CellType.Number); }
719 else if (t == typeof(decimal))
720 { c = new Cell((decimal)o, CellType.Number); }
721 else if (t == typeof(double))
722 { c = new Cell((double)o, CellType.Number); }
723 else if (t == typeof(float))
724 { c = new Cell((float)o, CellType.Number); }
725 else if (t == typeof(int))
726 { c = new Cell((int)o, CellType.Number); }
727 else if (t == typeof(uint))
728 { c = new Cell((uint)o, CellType.Number); }
729 else if (t == typeof(long))
730 { c = new Cell((long)o, CellType.Number); }
731 else if (t == typeof(ulong))
732 { c = new Cell((ulong)o, CellType.Number); }
733 else if (t == typeof(short))
734 { c = new Cell((short)o, CellType.Number); }
735 else if (t == typeof(ushort))
736 { c = new Cell((ushort)o, CellType.Number); }
737 else if (t == typeof(DateTime))
738 {
739 c = new Cell((DateTime)o, CellType.Date);
740 c.SetStyle(BasicStyles.DateFormat);
741 }
742 else if (t == typeof(TimeSpan))
743 {
744 c = new Cell((TimeSpan)o, CellType.Time);
745 c.SetStyle(BasicStyles.TimeFormat);
746 }
747 else if (t == typeof(string))
748 { c = new Cell((string)o, CellType.String); }
749 else // Default = unspecified object
750 {
751 c = new Cell(o.ToString(), CellType.Default);
752 }
753 output.Add(c);
754 }
755 return output;
756 }
757
765 public static IEnumerable<Address> GetCellRange(string range)
766 {
767 Range range2 = ResolveCellRange(range);
768 return GetCellRange(range2.StartAddress, range2.EndAddress);
769 }
770
779 public static IEnumerable<Address> GetCellRange(string startAddress, string endAddress)
780 {
781 Address start = ResolveCellCoordinate(startAddress);
782 Address end = ResolveCellCoordinate(endAddress);
783 return GetCellRange(start, end);
784 }
785
795 public static IEnumerable<Address> GetCellRange(int startColumn, int startRow, int endColumn, int endRow)
796 {
797 Address start = new Address(startColumn, startRow);
798 Address end = new Address(endColumn, endRow);
799 return GetCellRange(start, end);
800 }
801
810 public static IEnumerable<Address> GetCellRange(Address startAddress, Address endAddress)
811 {
812 int startColumn;
813 int endColumn;
814 int startRow;
815 int endRow;
816 if (startAddress.Column < endAddress.Column)
817 {
818 startColumn = startAddress.Column;
819 endColumn = endAddress.Column;
820 }
821 else
822 {
823 startColumn = endAddress.Column;
824 endColumn = startAddress.Column;
825 }
826 if (startAddress.Row < endAddress.Row)
827 {
828 startRow = startAddress.Row;
829 endRow = endAddress.Row;
830 }
831 else
832 {
833 startRow = endAddress.Row;
834 endRow = startAddress.Row;
835 }
836 List<Address> output = new List<Address>();
837 for (int column = startColumn; column <= endColumn; column++)
838 {
839 for (int row = startRow; row <= endRow; row++)
840 {
841 output.Add(new Address(column, row));
842 }
843 }
844 return output;
845 }
846
855 public static string ResolveCellAddress(int column, int row, AddressType type = AddressType.Default)
856 {
857 ValidateColumnNumber(column);
859 switch (type)
860 {
861 case AddressType.FixedRowAndColumn:
862 return "$" + ResolveColumnAddress(column) + "$" + (row + 1);
863 case AddressType.FixedColumn:
864 return "$" + ResolveColumnAddress(column) + (row + 1);
865 case AddressType.FixedRow:
866 return ResolveColumnAddress(column) + "$" + (row + 1);
867 default:
868 return ResolveColumnAddress(column) + (row + 1);
869 }
870 }
871
879 public static Address ResolveCellCoordinate(string address)
880 {
881 int row;
882 int column;
883 AddressType type;
884 ResolveCellCoordinate(address, out column, out row, out type);
885 return new Address(column, row, type);
886 }
887
896 public static void ResolveCellCoordinate(string address, out int column, out int row)
897 {
898 ResolveCellCoordinate(address, out column, out row, out _);
899 }
900
910 public static void ResolveCellCoordinate(string address, out int column, out int row, out AddressType addressType)
911 {
912 if (string.IsNullOrEmpty(address))
913 {
914 throw new FormatException("The cell address is null or empty and could not be resolved");
915 }
916
917 int i = 0;
918 int len = address.Length;
919 bool fixedCol = false;
920 bool fixedRow = false;
921
922 // Optional $ for column
923 if (i < len && address[i] == '$') { fixedCol = true; i++; }
924
925 // Column letters
926 int colStart = i;
927 while (i < len && ((address[i] >= 'A' && address[i] <= 'Z') || (address[i] >= 'a' && address[i] <= 'z')))
928 {
929 i++;
930 }
931 if (i == colStart)
932 {
933 throw new FormatException("The format of the cell address (" + address + ") is malformed");
934 }
935
936 string colPart = address.Substring(colStart, i - colStart);
937
938 // Optional $ for row
939 if (i < len && address[i] == '$') { fixedRow = true; i++; }
940
941 // Row digits
942 int rowStart = i;
943 while (i < len && address[i] >= '0' && address[i] <= '9')
944 {
945 i++;
946 }
947
948 if (i == rowStart || i != len)
949 {
950 throw new FormatException("The format of the cell address (" + address + ") is malformed");
951 }
952
953 row = int.Parse(address.Substring(rowStart, i - rowStart), NumberStyles.Integer, CultureInfo.InvariantCulture) - 1;
954 column = ResolveColumn(colPart);
956
957 if (fixedCol && fixedRow) { addressType = AddressType.FixedRowAndColumn; }
958 else if (fixedCol) { addressType = AddressType.FixedColumn; }
959 else if (fixedRow) { addressType = AddressType.FixedRow; }
960 else { addressType = AddressType.Default; }
961 }
962
970 public static Range ResolveCellRange(string range)
971 {
972 if (string.IsNullOrEmpty(range))
973 {
974 throw new FormatException("The cell range is null or empty and could not be resolved");
975 }
976 if (!range.Contains(":"))
977 {
978 return new Range(ResolveCellCoordinate(range), ResolveCellCoordinate(range));
979 }
980 string[] split = range.Split(':');
981 if (split.Length != 2)
982 {
983 throw new FormatException("The cell range (" + range + ") is malformed and could not be resolved");
984 }
985 return new Range(ResolveCellCoordinate(split[0]), ResolveCellCoordinate(split[1]));
986 }
987
994 public static int ResolveColumn(string columnAddress)
995 {
996 if (String.IsNullOrEmpty(columnAddress))
997 {
998 throw new RangeException("The passed address was null or empty");
999 }
1000 columnAddress = ParserUtils.ToUpper(columnAddress);
1001 int chr;
1002 int result = 0;
1003 int multiplier = 1;
1004 for (int i = columnAddress.Length - 1; i >= 0; i--)
1005 {
1006 chr = columnAddress[i];
1007 chr -= ASCII_OFFSET;
1008 result += (chr * multiplier);
1009 multiplier *= 26;
1010 }
1011 ValidateColumnNumber(result - 1);
1012 return result - 1;
1013 }
1014
1021 public static string ResolveColumnAddress(int columnNumber)
1022 {
1023 ValidateColumnNumber(columnNumber);
1024 // A - XFD
1025 StringBuilder sb = new StringBuilder();
1026 columnNumber++;
1027 while (columnNumber > 0)
1028 {
1029 columnNumber--;
1030 sb.Insert(0, (char)('A' + (columnNumber % 26)));
1031 columnNumber /= 26;
1032 }
1033 return sb.ToString();
1034 }
1035
1041 public static AddressScope GetAddressScope(string addressExpression)
1042 {
1043 try
1044 {
1045 ResolveCellCoordinate(addressExpression);
1046 return AddressScope.SingleAddress;
1047 }
1048 catch
1049 {
1050 try
1051 {
1052 ResolveCellRange(addressExpression);
1053 return AddressScope.Range;
1054 }
1055 catch
1056 {
1057 return AddressScope.Invalid;
1058 }
1059 }
1060
1061 }
1062
1068 public static void ValidateColumnNumber(int column)
1069 {
1070 if (column > Worksheet.MaxColumnNumber || column < Worksheet.MinColumnNumber)
1071 {
1072 throw new RangeException("The column number (" + column + ") is out of range. Range is from " +
1073 Worksheet.MinColumnNumber + " to " + Worksheet.MaxColumnNumber + " (" + (Worksheet.MaxColumnNumber + 1) + " columns).");
1074 }
1075 }
1076
1082 public static void ValidateRowNumber(int row)
1083 {
1084 if (row > Worksheet.MaxRowNumber || row < Worksheet.MinRowNumber)
1085 {
1086 throw new RangeException("The row number (" + row + ") is out of range. Range is from " +
1087 Worksheet.MinRowNumber + " to " + Worksheet.MaxRowNumber + " (" + (Worksheet.MaxRowNumber + 1) + " rows).");
1088 }
1089 }
1090
1095 internal void BindFeatures(FeatureSet features)
1096 {
1097 if (ReferenceEquals(worksheetFeatures, features))
1098 {
1099 return;
1100 }
1101 UnbindFeatures();
1102 worksheetFeatures = features;
1103 AttachFormulaFeatures();
1104 }
1105
1109 internal void UnbindFeatures()
1110 {
1111 DetachFormulaFeatures();
1112 worksheetFeatures = null;
1113 }
1114
1118 private void SynchronitzeValueFromFormula()
1119 {
1120 if (formula.MasterCellAddress == null)
1121 {
1122 this.value = formula.Expression; // Sync back from formula to cell
1123 }
1124 }
1125
1129 private void AttachFormulaFeatures()
1130 {
1131 if (worksheetFeatures != null && dataType == CellType.Formula && formula != null)
1132 {
1133 formula.Features.Add(worksheetFeatures);
1134 }
1135 }
1136
1140 private void ClearFormula()
1141 {
1142 DetachFormulaFeatures();
1143 formula = null;
1144 }
1145
1149 private void DetachFormulaFeatures()
1150 {
1151 if (worksheetFeatures != null && dataType == CellType.Formula && formula != null)
1152 {
1153 formula.Features.Remove(worksheetFeatures);
1154 }
1155 }
1156
1161 private string GetValueAsFormulaExpression()
1162 {
1163 return value == null ? null : value.ToString();
1164 }
1165
1171 private Range TransposeDefinedNameArrayRange(string referenceExpression)
1172 {
1173 Range resolvedRange = new Range(referenceExpression);
1174 int rowCount = resolvedRange.EndAddress.Row - resolvedRange.StartAddress.Row;
1175 int columnCount = resolvedRange.EndAddress.Column - resolvedRange.StartAddress.Column;
1176 return new Range(this.ColumnNumber, this.rowNumber, this.ColumnNumber + columnCount, this.rowNumber + rowCount);
1177 }
1178
1179 #endregion
1180
1181 }
1182}
Class representing a cell of a worksheet.
Definition Cell.cs:25
static Address ResolveCellCoordinate(string address)
Gets the column and row number (zero based) of a cell by the address.
Definition Cell.cs:879
static bool operator==(Cell left, Cell right)
Determines whether two Cell instances are equal.
Definition Cell.cs:605
override int GetHashCode()
Gets the hash code of the cell.
Definition Cell.cs:583
static IEnumerable< Address > GetCellRange(Address startAddress, Address endAddress)
Get a list of cell addresses from a cell range.
Definition Cell.cs:810
static IEnumerable< Address > GetCellRange(int startColumn, int startRow, int endColumn, int endRow)
Get a list of cell addresses from a cell range.
Definition Cell.cs:795
CellType
Enum defines the basic data types of a cell.
Definition Cell.cs:35
@ String
Type for single characters and strings.
Definition Cell.cs:38
@ Date
Type for dates (Note: Dates before 1900-01-01 and after 9999-12-31 are not allowed).
Definition Cell.cs:43
@ Default
Default Type, not specified.
Definition Cell.cs:60
@ Error
Type for a standalone error value in a cell.
Definition Cell.cs:58
@ Time
Type for times (Note: Internally handled as OAdate, represented by TimeSpan).
Definition Cell.cs:45
@ Number
Type for all numeric types (long, integer, float, double, short, byte and decimal; signed and unsigne...
Definition Cell.cs:41
@ Bool
Type for boolean.
Definition Cell.cs:47
@ Empty
Type for empty cells. This type is only used for merged cells (all cells except the first of the cell...
Definition Cell.cs:51
Cell()
Default constructor. Cells created with this constructor do not have a link to a worksheet initially.
Definition Cell.cs:261
static IEnumerable< Cell > ConvertArray< T >(IEnumerable< T > list)
Converts a List of supported objects into a list of cells.
Definition Cell.cs:691
void SetCellLockedState(bool isLocked, bool isHidden)
Sets the lock state of the cell.
Definition Cell.cs:527
static void ValidateRowNumber(int row)
Validates the passed (zero-based) row number. An exception will be thrown if the row is invalid.
Definition Cell.cs:1082
Address CellAddress2
Gets or sets the combined cell Address as Address object.
Definition Cell.cs:123
static string ResolveCellAddress(int column, int row, AddressType type=AddressType.Default)
Gets the address of a cell by the column and row number (zero based).
Definition Cell.cs:855
static AddressScope GetAddressScope(string addressExpression)
Gets the scope of the passed address (string expression). Scope means either single cell address or r...
Definition Cell.cs:1041
static bool operator<(Cell left, Cell right)
Determines whether the first instance of a Cell is less/smaller as the second.
Definition Cell.cs:635
AddressType
Enum for the referencing style of the address.
Definition Cell.cs:67
@ FixedColumn
Column of the address is fixed (e.g. '$C3').
Definition Cell.cs:73
@ FixedRow
Row of the address is fixed (e.g. 'C$3').
Definition Cell.cs:71
@ FixedRowAndColumn
Row and column of the address is fixed (e.g. '$C$3').
Definition Cell.cs:75
Style CellStyle
Gets the assigned style of the cell.
Definition Cell.cs:137
static bool operator>=(Cell left, Cell right)
Determines whether the first instance of a Cell is greater/larger or equal as the second.
Definition Cell.cs:677
static IEnumerable< Address > GetCellRange(string startAddress, string endAddress)
Get a list of cell addresses from a cell range.
Definition Cell.cs:779
static Range ResolveCellRange(string range)
Resolves a cell range from the format like A1:B3 or AAD556:AAD1000.
Definition Cell.cs:970
int RowNumber
Gets or sets the number of the row (zero-based).
Definition Cell.cs:191
static void ValidateColumnNumber(int column)
Validates the passed (zero-based) column number. An exception will be thrown if the column is invalid...
Definition Cell.cs:1068
static int ResolveColumn(string columnAddress)
Gets the column number from the column address (A - XFD).
Definition Cell.cs:994
CellType DataType
Gets or sets the type of the cell.
Definition Cell.cs:158
static bool operator!=(Cell left, Cell right)
Determines whether two Cell instances are not equal.
Definition Cell.cs:621
override bool Equals(object obj)
Compares two objects whether they are addresses and equal.
Definition Cell.cs:388
string CellAddress
Gets or sets the combined cell Address as string in the format A1 - XFD1048576. The address may conta...
Definition Cell.cs:111
Cell(object value, CellType type, string address)
Constructor with value, cell type and address as string. The worksheet reference is set to null and m...
Definition Cell.cs:296
AddressScope
Enum to define the scope of a passed address string (used in static context).
Definition Cell.cs:82
@ SingleAddress
The address represents a single cell.
Definition Cell.cs:86
@ Invalid
The address expression is invalid.
Definition Cell.cs:90
@ Range
The address represents a range of cells.
Definition Cell.cs:88
@ Any
The address represents a single cell or a range of cells.
Definition Cell.cs:84
FormulaData Formula
Formula object in case of the cell has the DataType CellType.Formula. Default is null,...
Definition Cell.cs:242
static void ResolveCellCoordinate(string address, out int column, out int row, out AddressType addressType)
Gets the column and row number (zero based) of a cell by the address.
Definition Cell.cs:910
Cell(object value, CellType type, Address address)
Constructor with value, cell type and address as struct. The worksheet reference is set to null and m...
Definition Cell.cs:321
int ColumnNumber
Gets or sets the number of the column (zero-based).
Definition Cell.cs:144
Cell(object value, CellType type, int column, int row)
Constructor with value, cell type, row number and column number.
Definition Cell.cs:348
Cell(object value, CellType type)
Constructor with value and cell type. Cells created with this constructor do not have a link to a wor...
Definition Cell.cs:272
Style SetStyle(Style style, bool unmanaged=false)
Sets the style of the cell.
Definition Cell.cs:549
static IEnumerable< Address > GetCellRange(string range)
Gets a list of cell addresses from a cell range (format A1:B3 or AAD556:AAD1000).
Definition Cell.cs:765
void ResolveCellType()
Method resets the Cell type and tries to find the actual type. This is used if a Cell was created wit...
Definition Cell.cs:477
AddressType CellAddressType
Gets or sets the optional address type that can be part of the cell address.
Definition Cell.cs:204
static bool operator>(Cell left, Cell right)
Determines whether the first instance of a Cell is greater/larger as the second.
Definition Cell.cs:663
static void ResolveCellCoordinate(string address, out int column, out int row)
Gets the column and row number (zero based) of a cell by the address.
Definition Cell.cs:896
object Value
Gets or sets the value of the cell (generic object type). When setting a value, the DataType is autom...
Definition Cell.cs:212
static string ResolveColumnAddress(int columnNumber)
Gets the column address (A - XFD).
Definition Cell.cs:1021
static bool operator<=(Cell left, Cell right)
Determines whether the first instance of a Cell is less/smaller or equal as the second.
Definition Cell.cs:649
int CompareTo(Cell other)
Implemented CompareTo method.
Definition Cell.cs:369
void RemoveStyle()
Removes the assigned style from the cell.
Definition Cell.cs:421
Class representing a defined name within a workbook. A defined name is a descriptive text that repres...
object Value
Gets the raw reference of the defined Name. The value will be transformed in its appropriate text val...
string TextValue
Gets the textual reference of the defined name. This is stored verbatim and may be a cell address (e....
NameType Type
Type of the defined name.
string Name
Gets the name of the defined name as it appears in the workbook (e.g. MyRange).
NameType
Enum to specify the type of the defined name.
Static class that contains shared enums for error cases.
Definition Errors.cs:16
FormulaError
Errors that can occur in formulas / functions.
Definition Errors.cs:21
Class for exceptions regarding format error incidents.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding Style incidents.
Class for exceptions regarding worksheet incidents.
Class representing a formula in a cell, its data, respectively.
bool Equals(FormulaData other)
Determines whether the specified FormulaData instance is equal to this instance.
FormulaType
Enum to define the specific type of a formal if the Cell has the type Cell.CellType....
bool Equals(AbstractStyle other)
Method to compare two objects for sorting purpose.
Factory class with the most important predefined styles.
static Style TimeFormat
Gets the time format style.
static Style DateFormat
Gets the date format style.
Class to manage all styles at runtime, before writing XLSX files. The main purpose is deduplication a...
static StyleRepository Instance
Gets the singleton instance of the repository.
Style AddStyle(Style style)
Adds a style to the repository and returns the actual reference.
Class representing a Style with sub classes within a style sheet. An instance of this class is only a...
Definition Style.cs:18
Style CopyStyle()
Method to copy the current object to a new one with casting.
Definition Style.cs:227
Class providing static methods to parse string values to specific types or to print object as languag...
static string ToUpper(string input)
Transforms a string to upper case with null check and invariant culture.
Class representing a worksheet of a workbook.
Definition Worksheet.cs:27
static readonly int MinColumnNumber
Minimum column number (zero-based) as constant.
Definition Worksheet.cs:54
static readonly int MaxRowNumber
Maximum row number (zero-based) as constant.
Definition Worksheet.cs:75
static readonly int MaxColumnNumber
Maximum column number (zero-based) as constant.
Definition Worksheet.cs:49
static readonly int MinRowNumber
Minimum row number (zero-based) as constant.
Definition Worksheet.cs:80
Struct representing the cell address as column and row (zero based).
Definition Address.cs:16
int Row
Row number (zero based).
Definition Address.cs:28
Cell.AddressType Type
Referencing type of the address.
Definition Address.cs:33
bool Equals(Address other)
Compares two addresses whether they are equal.
Definition Address.cs:112
int Column
Column number (zero based).
Definition Address.cs:24
Struct representing a cell range with a start and end address.
Definition Range.cs:16
Address StartAddress
Start address of the range.
Definition Range.cs:27
Address EndAddress
End address of the range.
Definition Range.cs:23