9using System.Collections.Generic;
10using System.Globalization;
22 public class Cell : IComparable<Cell>
25 private const int ASCII_OFFSET = 64;
35#pragma warning disable CA1720
37#pragma warning restore CA1720
85 private Style cellStyle;
86 private int columnNumber;
87 private int rowNumber;
124 get {
return cellStyle; }
131 get {
return columnNumber; }
135 columnNumber = value;
147 get {
return rowNumber; }
248 columnNumber = address.
Column;
249 rowNumber = address.
Row;
264 public Cell(
object value,
CellType type,
int column,
int row) : this(value, type)
306 if (obj ==
null || obj.GetType() != typeof(
Cell))
319 if (this.DataType != other.
DataType)
323 if (this.Value !=
null && other.
Value !=
null && !
this.Value.Equals(other.
Value))
344 if (this.value ==
null)
352 Type t = this.value.GetType();
353 if (t == typeof(
bool))
355 else if (t == typeof(
byte) || t == typeof(sbyte))
357 else if (t == typeof(decimal))
359 else if (t == typeof(
double))
361 else if (t == typeof(
float))
363 else if (t == typeof(
int) || t == typeof(uint))
365 else if (t == typeof(
long) || t == typeof(ulong))
367 else if (t == typeof(
short) || t == typeof(ushort))
369 else if (t == typeof(DateTime))
375 else if (t == typeof(TimeSpan))
393 if (cellStyle ==
null)
395 lockStyle =
new Style();
401 lockStyle.CurrentCellXf.Locked = isLocked;
402 lockStyle.CurrentCellXf.Hidden = isHidden;
435 if (this.cellStyle !=
null)
437 copy.
SetStyle(this.cellStyle,
true);
450 hash = hash * 31 + columnNumber.GetHashCode();
451 hash = hash * 31 + rowNumber.GetHashCode();
453 hash = hash * 31 +
DataType.GetHashCode();
454 hash = hash * 31 + (cellStyle?.GetHashCode() ?? 0);
455 hash = hash * 31 + (value?.GetHashCode() ?? 0);
468 if (ReferenceEquals(left,
null))
470 return ReferenceEquals(right,
null);
473 return left.
Equals(right);
484 return !(left == right);
498 return ReferenceEquals(left,
null) ? !ReferenceEquals(right,
null) : left.
CompareTo(right) < 0;
512 return ReferenceEquals(left,
null) || left.
CompareTo(right) <= 0;
526 return !ReferenceEquals(left,
null) && left.
CompareTo(right) > 0;
540 return ReferenceEquals(left,
null) ? ReferenceEquals(right,
null) : left.
CompareTo(right) >= 0;
545 #region staticMethods
554 List<Cell> output =
new List<Cell>();
562 foreach (T item
in list)
572 if (t == typeof(
Cell))
573 { c = item as
Cell; }
574 else if (t == typeof(
bool))
576 else if (t == typeof(
byte))
578 else if (t == typeof(sbyte))
580 else if (t == typeof(decimal))
582 else if (t == typeof(
double))
584 else if (t == typeof(
float))
586 else if (t == typeof(
int))
588 else if (t == typeof(uint))
590 else if (t == typeof(
long))
592 else if (t == typeof(ulong))
594 else if (t == typeof(
short))
596 else if (t == typeof(ushort))
598 else if (t == typeof(DateTime))
603 else if (t == typeof(TimeSpan))
608 else if (t == typeof(
string))
640 public static IEnumerable<Address>
GetCellRange(
string startAddress,
string endAddress)
656 public static IEnumerable<Address>
GetCellRange(
int startColumn,
int startRow,
int endColumn,
int endRow)
679 startColumn = startAddress.
Column;
680 endColumn = endAddress.
Column;
684 startColumn = endAddress.
Column;
685 endColumn = startAddress.
Column;
687 if (startAddress.
Row < endAddress.
Row)
689 startRow = startAddress.
Row;
690 endRow = endAddress.
Row;
694 startRow = endAddress.
Row;
695 endRow = startAddress.
Row;
697 List<Address> output =
new List<Address>();
698 for (
int column = startColumn; column <= endColumn; column++)
700 for (
int row = startRow; row <= endRow; row++)
702 output.Add(
new Address(column, row));
746 return new Address(column, row, type);
773 if (
string.IsNullOrEmpty(address))
775 throw new FormatException(
"The cell address is null or empty and could not be resolved");
779 int len = address.Length;
780 bool fixedCol =
false;
781 bool fixedRow =
false;
784 if (i < len && address[i] ==
'$') { fixedCol =
true; i++; }
788 while (i < len && ((address[i] >=
'A' && address[i] <=
'Z') || (address[i] >=
'a' && address[i] <=
'z')))
794 throw new FormatException(
"The format of the cell address (" + address +
") is malformed");
797 string colPart = address.Substring(colStart, i - colStart);
800 if (i < len && address[i] ==
'$') { fixedRow =
true; i++; }
804 while (i < len && address[i] >=
'0' && address[i] <=
'9')
809 if (i == rowStart || i != len)
811 throw new FormatException(
"The format of the cell address (" + address +
") is malformed");
814 row =
int.Parse(address.Substring(rowStart, i - rowStart), NumberStyles.Integer, CultureInfo.InvariantCulture) - 1;
818 if (fixedCol && fixedRow) { addressType =
AddressType.FixedRowAndColumn; }
819 else if (fixedCol) { addressType =
AddressType.FixedColumn; }
820 else if (fixedRow) { addressType =
AddressType.FixedRow; }
833 if (
string.IsNullOrEmpty(range))
835 throw new FormatException(
"The cell range is null or empty and could not be resolved");
837 if (!range.Contains(
":"))
841 string[] split = range.Split(
':');
842 if (split.Length != 2)
844 throw new FormatException(
"The cell range (" + range +
") is malformed and could not be resolved");
857 if (
String.IsNullOrEmpty(columnAddress))
865 for (
int i = columnAddress.Length - 1; i >= 0; i--)
867 chr = columnAddress[i];
869 result += (chr * multiplier);
886 StringBuilder sb =
new StringBuilder();
888 while (columnNumber > 0)
891 sb.Insert(0, (
char)(
'A' + (columnNumber % 26)));
894 return sb.ToString();
933 throw new RangeException(
"The column number (" + column +
") is out of range. Range is from " +
947 throw new RangeException(
"The row number (" + row +
") is out of range. Range is from " +
Class representing a cell of a worksheet.
static Address ResolveCellCoordinate(string address)
Gets the column and row number (zero based) of a cell by the address.
static bool operator==(Cell left, Cell right)
Determines whether two Cell instances are equal.
override int GetHashCode()
Gets the hash code of the cell.
static IEnumerable< Address > GetCellRange(Address startAddress, Address endAddress)
Get a list of cell addresses from a cell range.
static IEnumerable< Address > GetCellRange(int startColumn, int startRow, int endColumn, int endRow)
Get a list of cell addresses from a cell range.
CellType
Enum defines the basic data types of a cell.
@ String
Type for single characters and strings.
@ Formula
Type for Formulas (The cell will be handled differently).
@ Date
Type for dates (Note: Dates before 1900-01-01 and after 9999-12-31 are not allowed).
@ Default
Default Type, not specified.
@ Time
Type for times (Note: Internally handled as OAdate, represented by TimeSpan).
@ Number
Type for all numeric types (long, integer, float, double, short, byte and decimal; signed and unsigne...
@ Empty
Type for empty cells. This type is only used for merged cells (all cells except the first of the cell...
Cell()
Default constructor. Cells created with this constructor do not have a link to a worksheet initially.
static IEnumerable< Cell > ConvertArray< T >(IEnumerable< T > list)
Converts a List of supported objects into a list of cells.
void SetCellLockedState(bool isLocked, bool isHidden)
Sets the lock state of the cell.
static void ValidateRowNumber(int row)
Validates the passed (zero-based) row number. An exception will be thrown if the row is invalid.
Address CellAddress2
Gets or sets the combined cell Address as Address object.
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).
static AddressScope GetAddressScope(string addressExpression)
Gets the scope of the passed address (string expression). Scope means either single cell address or r...
static bool operator<(Cell left, Cell right)
Determines whether the first instance of a Cell is less/smaller as the second.
AddressType
Enum for the referencing style of the address.
@ FixedColumn
Column of the address is fixed (e.g. '$C3').
@ FixedRow
Row of the address is fixed (e.g. 'C$3').
@ FixedRowAndColumn
Row and column of the address is fixed (e.g. '$C$3').
Style CellStyle
Gets the assigned style of the cell.
static bool operator>=(Cell left, Cell right)
Determines whether the first instance of a Cell is greater/larger or equal as the second.
static IEnumerable< Address > GetCellRange(string startAddress, string endAddress)
Get a list of cell addresses from a cell range.
static Range ResolveCellRange(string range)
Resolves a cell range from the format like A1:B3 or AAD556:AAD1000.
int RowNumber
Gets or sets the number of the row (zero-based).
static void ValidateColumnNumber(int column)
Validates the passed (zero-based) column number. An exception will be thrown if the column is invalid...
static int ResolveColumn(string columnAddress)
Gets the column number from the column address (A - XFD).
CellType DataType
Gets or sets the type of the cell.
static bool operator!=(Cell left, Cell right)
Determines whether two Cell instances are not equal.
override bool Equals(object obj)
Compares two objects whether they are addresses and equal.
string CellAddress
Gets or sets the combined cell Address as string in the format A1 - XFD1048576. The address may conta...
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...
AddressScope
Enum to define the scope of a passed address string (used in static context).
@ SingleAddress
The address represents a single cell.
@ Invalid
The address expression is invalid.
@ Range
The address represents a range of cells.
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.
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...
int ColumnNumber
Gets or sets the number of the column (zero-based).
Cell(object value, CellType type, int column, int row)
Constructor with value, cell type, row number and column number.
Cell(object value, CellType type)
Constructor with value and cell type. Cells created with this constructor do not have a link to a wor...
Style SetStyle(Style style, bool unmanaged=false)
Sets the style of the cell.
static IEnumerable< Address > GetCellRange(string range)
Gets a list of cell addresses from a cell range (format A1:B3 or AAD556:AAD1000).
void ResolveCellType()
Method resets the Cell type and tries to find the actual type. This is used if a Cell was created wit...
AddressType CellAddressType
Gets or sets the optional address type that can be part of the cell address.
static bool operator>(Cell left, Cell right)
Determines whether the first instance of a Cell is greater/larger as the second.
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.
object Value
Gets or sets the value of the cell (generic object type). When setting a value, the DataType is autom...
static string ResolveColumnAddress(int columnNumber)
Gets the column address (A - XFD).
static bool operator<=(Cell left, Cell right)
Determines whether the first instance of a Cell is less/smaller or equal as the second.
int CompareTo(Cell other)
Implemented CompareTo method.
void RemoveStyle()
Removes the assigned style from the cell.
Class for exceptions regarding range incidents (e.g. out-of-range).
Class for exceptions regarding Style incidents.
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...
Style CopyStyle()
Method to copy the current object to a new one with casting.
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.
static readonly int MinColumnNumber
Minimum column number (zero-based) as constant.
static readonly int MaxRowNumber
Maximum row number (zero-based) as constant.
static readonly int MaxColumnNumber
Maximum column number (zero-based) as constant.
static readonly int MinRowNumber
Minimum row number (zero-based) as constant.
Struct representing the cell address as column and row (zero based).
int Row
Row number (zero based).
Cell.AddressType Type
Referencing type of the address.
bool Equals(Address other)
Compares two addresses whether they are equal.
int Column
Column number (zero based).
Struct representing a cell range with a start and end address.
Address StartAddress
Start address of the range.
Address EndAddress
End address of the range.