9using System.Collections.Generic;
10using System.Globalization;
12using System.Text.RegularExpressions;
23 public class Cell : IComparable<Cell>
26 private const int ASCII_OFFSET = 64;
37#pragma warning disable CA1720
39#pragma warning restore CA1720
87 private Style cellStyle;
88 private int columnNumber;
89 private int rowNumber;
126 get {
return cellStyle; }
133 get {
return columnNumber; }
137 columnNumber = value;
149 get {
return rowNumber; }
250 columnNumber = address.
Column;
251 rowNumber = address.
Row;
266 public Cell(
object value,
CellType type,
int column,
int row) : this(value, type)
308 if (obj ==
null || obj.GetType() != typeof(
Cell))
321 if (this.DataType != other.
DataType)
325 if (this.Value !=
null && other.
Value !=
null && !
this.Value.Equals(other.
Value))
346 if (this.value ==
null)
354 Type t = this.value.GetType();
355 if (t == typeof(
bool))
357 else if (t == typeof(
byte) || t == typeof(sbyte))
359 else if (t == typeof(decimal))
361 else if (t == typeof(
double))
363 else if (t == typeof(
float))
365 else if (t == typeof(
int) || t == typeof(uint))
367 else if (t == typeof(
long) || t == typeof(ulong))
369 else if (t == typeof(
short) || t == typeof(ushort))
371 else if (t == typeof(DateTime))
377 else if (t == typeof(TimeSpan))
395 if (cellStyle ==
null)
397 lockStyle =
new Style();
403 lockStyle.CurrentCellXf.Locked = isLocked;
404 lockStyle.CurrentCellXf.Hidden = isHidden;
437 if (this.cellStyle !=
null)
439 copy.
SetStyle(this.cellStyle,
true);
452 hash = hash * 31 + columnNumber.GetHashCode();
453 hash = hash * 31 + rowNumber.GetHashCode();
455 hash = hash * 31 +
DataType.GetHashCode();
456 hash = hash * 31 + (cellStyle?.GetHashCode() ?? 0);
457 hash = hash * 31 + (value?.GetHashCode() ?? 0);
470 if (ReferenceEquals(left,
null))
472 return ReferenceEquals(right,
null);
475 return left.
Equals(right);
486 return !(left == right);
500 return ReferenceEquals(left,
null) ? !ReferenceEquals(right,
null) : left.
CompareTo(right) < 0;
514 return ReferenceEquals(left,
null) || left.
CompareTo(right) <= 0;
528 return !ReferenceEquals(left,
null) && left.
CompareTo(right) > 0;
542 return ReferenceEquals(left,
null) ? ReferenceEquals(right,
null) : left.
CompareTo(right) >= 0;
547 #region staticMethods
556 List<Cell> output =
new List<Cell>();
564 foreach (T item
in list)
574 if (t == typeof(
Cell))
575 { c = item as
Cell; }
576 else if (t == typeof(
bool))
578 else if (t == typeof(
byte))
580 else if (t == typeof(sbyte))
582 else if (t == typeof(decimal))
584 else if (t == typeof(
double))
586 else if (t == typeof(
float))
588 else if (t == typeof(
int))
590 else if (t == typeof(uint))
592 else if (t == typeof(
long))
594 else if (t == typeof(ulong))
596 else if (t == typeof(
short))
598 else if (t == typeof(ushort))
600 else if (t == typeof(DateTime))
605 else if (t == typeof(TimeSpan))
610 else if (t == typeof(
string))
642 public static IEnumerable<Address>
GetCellRange(
string startAddress,
string endAddress)
658 public static IEnumerable<Address>
GetCellRange(
int startColumn,
int startRow,
int endColumn,
int endRow)
681 startColumn = startAddress.
Column;
682 endColumn = endAddress.
Column;
686 startColumn = endAddress.
Column;
687 endColumn = startAddress.
Column;
689 if (startAddress.
Row < endAddress.
Row)
691 startRow = startAddress.
Row;
692 endRow = endAddress.
Row;
696 startRow = endAddress.
Row;
697 endRow = startAddress.
Row;
699 List<Address> output =
new List<Address>();
700 for (
int column = startColumn; column <= endColumn; column++)
702 for (
int row = startRow; row <= endRow; row++)
704 output.Add(
new Address(column, row));
748 return new Address(column, row, type);
776 if (
string.IsNullOrEmpty(address))
778 throw new FormatException(
"The cell address is null or empty and could not be resolved");
781 Regex pattern =
new Regex(
"(^(\\$?)([A-Z]{1,3})(\\$?)([0-9]{1,7})$)");
782 Match matcher = pattern.Match(address);
783 if (matcher.Groups.Count != 6)
785 throw new FormatException(
"The format of the cell address (" + address +
") is malformed");
787 int digits =
int.Parse(matcher.Groups[5].Value, CultureInfo.InvariantCulture);
791 if (!
String.IsNullOrEmpty(matcher.Groups[2].Value) && !
String.IsNullOrEmpty(matcher.Groups[4].Value))
795 else if (!
String.IsNullOrEmpty(matcher.Groups[2].Value) &&
String.IsNullOrEmpty(matcher.Groups[4].Value))
799 else if (
String.IsNullOrEmpty(matcher.Groups[2].Value) && !
String.IsNullOrEmpty(matcher.Groups[4].Value))
818 if (
string.IsNullOrEmpty(range))
820 throw new FormatException(
"The cell range is null or empty and could not be resolved");
822 if (!range.Contains(
":"))
826 string[] split = range.Split(
':');
827 if (split.Length != 2)
829 throw new FormatException(
"The cell range (" + range +
") is malformed and could not be resolved");
842 if (
String.IsNullOrEmpty(columnAddress))
850 for (
int i = columnAddress.Length - 1; i >= 0; i--)
852 chr = columnAddress[i];
854 result += (chr * multiplier);
871 StringBuilder sb =
new StringBuilder();
873 while (columnNumber > 0)
876 sb.Insert(0, (
char)(
'A' + (columnNumber % 26)));
879 return sb.ToString();
918 throw new RangeException(
"The column number (" + column +
") is out of range. Range is from " +
932 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.