using System; namespace FlexFramework.Excel { /// /// Data cell /// public class Cell : ICloneable { /// /// Cell address /// public Address Address { get; set; } /// /// Cell value /// public object Value { get; set; } /// /// If this cell is a span cell(which has no value until is called) /// public bool IsSpan { get; set; } public Cell() { } public Cell(Address address) : this() { Address = address; } public Cell(string value, Address address) : this(address) { Value = value; } public Cell(bool value, Address address) : this(address) { Value = value; } public Cell(int value, Address address) : this(address) { Value = value; } public Cell(long value, Address address) : this(address) { Value = value; } public Cell(float value, Address address) : this(address) { Value = value; } public Cell(double value, Address address) : this(address) { Value = value; } /// /// Get plain text(value cast to string) /// /// /// Null value will return an empty string /// public virtual string Text { get { return Convert.ToString(Value) ?? string.Empty; } } /// /// Get string value if possible; otherwise an exception will be thrown /// /// public string String { get { if (!IsString) throw new InvalidCastException(); return (string)Value; } } /// /// Get int value if possible; otherwise an exception will be thrown /// /// public int Integer { get { if (!IsInteger) throw new InvalidCastException(); return (int)Value; } } /// /// Get bool value if possible; otherwise an exception will be thrown /// /// public bool Boolean { get { if (!IsBoolean) throw new InvalidCastException(); return (bool)Value; } } /// /// Get float value if possible; otherwise an exception will be thrown /// /// public float Single { get { if (!IsSingle) throw new InvalidCastException(); return (float)Value; } } /// /// Get double value if possible; otherwise an exception will be thrown /// /// public double Double { get { if (!IsDouble) throw new InvalidCastException(); return (double)Value; } } /// /// Check if this cell's value type is /// public bool IsInteger { get { return Value is int; } } /// /// Check if this cell's value type is /// public bool IsSingle { get { return Value is float; } } /// /// Check if this cell's value type is /// public bool IsDouble { get { return Value is double; } } /// /// Check if this cell's value type is /// public bool IsBoolean { get { return Value is bool; } } /// /// Check if this cell's value type is /// public bool IsString { get { return Value is string; } } public override string ToString() { return Text; } public Cell DeepClone() { return (Cell)MemberwiseClone(); } public Cell ShallowClone() { return (Cell)MemberwiseClone(); } public static implicit operator string(Cell cell) { return cell.String; } public static implicit operator int(Cell cell) { return cell.Integer; } public static implicit operator bool(Cell cell) { return cell.Boolean; } public static implicit operator float(Cell cell) { return cell.Single; } public static implicit operator double(Cell cell) { return cell.Double; } } }