namespace com.azkoss.excellite
{
using System;
using System.Drawing;
///
///Excel cell provides access to a single worksheet cell or to a merged range if the cell is merged.
///
///
///Merged range is created by using CellRange.Merged property.
///See the property documentation for more information on merging.
///
///CellRange.Merged
///
public sealed class ExcelCell : AbstractRange
{
// Methods
internal ExcelCell(ExcelWorksheet parent) : base(parent)
{
}
internal ExcelCell(ExcelWorksheet parent, ExcelCell sourceCell) : base(parent)
{
this.cellValue = sourceCell.ValueInternal;
this.Style = sourceCell.Style;
}
internal void AddToMergedRange(MergedCellRange mergedRange)
{
if ((mergedRange.Value == null) && (this.cellValue != null))
{
mergedRange.ValueInternal = this.cellValue;
if ((this.style != null) && !this.style.IsDefault)
{
mergedRange.Style = this.style;
}
}
this.AddToMergedRangeInternal(mergedRange);
}
internal void AddToMergedRangeInternal(MergedCellRange mergedRange)
{
this.cellValue = mergedRange;
}
///
///Converts Excel floating-point number to DateTime structure.
///
///
///
///Excel file format doesn't have a separate data type for date and time.
///DateTime value is
///stored as IEEE number encoded in a special way. When reading Excel file,
///CellStyle.NumberFormat is examined and if it matches
///some of date/time number formats cell value is interpreted as DateTime.
///However, if some non-standard date/time number format is used, cell value will not be recognized
///as DateTime but as ordinary number. In such cases (when you know that
///specific cell holds DateTime value but you get a number when reading
///Excel file) use this method to convert IEEE number to DateTime
///structure.
///
///Excel floating-point number.
///Converted DateTime structure.
public static DateTime ConvertExcelNumberToDateTime(double num)
{
DateTime time1 = new DateTime(0x76c, 1, 1);
if (double.IsNaN(num) || double.IsInfinity(num))
{
return time1;
}
long num1 = (long) num;
long num2 = (long) ((num - num1) * 86400);
DateTime time2 = time1.AddDays((double) (num1 - 2));
return time2.AddSeconds((double) num2);
}
internal void RemoveFromMergedRange()
{
MergedCellRange range1 = this.cellValue as MergedCellRange;
if (range1 == null)
{
throw new Exception("Internal error: cell is not merged.");
}
this.cellValue = range1.ValueInternal;
this.style = range1.Style;
}
///
///Sets borders on this cell or on merged range if this cell is merged.
///
///Borders to set.
///Line color.
///Line style.
///CellRange.Merged
///
public override void SetBorders(MultipleBorders multipleBorders, Color lineColor, LineStyle lineStyle)
{
this.Style.Borders.SetBorders(multipleBorders, lineColor, lineStyle);
}
// Properties
///
///Gets or sets cell formula string.
///
///
///ExcelLite can read and write formulas, but can not calculate formula results. When you open a XLS file in
///MS Excel, formula results will be calculated automaticaly.
///During setting formula string ExcelLite formula parser will use English culture to parse numbers.
///Currently supported formula features are:
///
///- Named cell
///- Named range
///- Absolute cell/range
///- Relative cell/range
///- Functions( partly, see the list of supported functions below )
///- Missed argument
///- Unary operator
///- Binary operator
///- Parentheses
///- 3d cell reference
///- 3d cell range reference
///- Boolean
///- Integer
///- Float
///- String
///- Error
///
///
///
///Currently unsupported formula features are:
///
///- Const array
///- Array formula
///- R1C1 reference
///
///
///
///Currently supported functions are:
///
///- NOW
///- SECOND
///- MINUTE
///- HOUR
///- WEEKDAY
///- YEAR
///- MONTH
///- DAY
///- TIME
///- DATE
///- RAND
///- TEXT
///- VAR
///- MOD
///- NOT
///- OR
///- AND
///- FALSE
///- TRUE
///- VALUE
///- LEN
///- MID
///- ROUND
///- SIGN
///- INT
///- ABS
///- LN
///- EXP
///- SQRT
///- PI
///- COS
///- SIN
///- COLUMN
///- ROW
///- MAX
///- MIN
///- AVERAGE
///- SUM
///- IF
///- COUNT
///
///
///
///For more information on formulas, consult Microsoft Excel documentation.
///
///
///Following code demonstrates how to use formulas and named ranges. It shows next features:
///cell references (both absolute and relative), unary and binary operators, constand operands (integer and floating point),
///functions and named cell ranges.
///
///ws.Cells("A1").Value = 5
///ws.Cells("A2").Value = 6
///ws.Cells("A3").Value = 10
///
///ws.Cells("C1").Formula = "=A1+A2"
///ws.Cells("C2").Formula = "=$A$1-A3"
///ws.Cells("C3").Formula = "=COUNT(A1:A3)"
///ws.Cells("C4").Formula = "=AVERAGE($A$1:$A$3)"
///ws.Cells("C5").Formula = "=SUM(A1:A3,2,3)"
///ws.Cells("C7").Formula = "= 123 - (-(-(23.5)))"
///
///ws.NamedRanges.Add("DataRange", ws.Cells.GetSubrange("A1", "A3"))
///ws.Cells("C8").Formula = "=MAX(DataRange)"
///
///Dim cr As CellRange = ws.Cells.GetSubrange("B9","C10")
///cr.Merged = True
///cr.Formula = "=A1*25"
///
///
///ws.Cells["A1"].Value = 5;
///ws.Cells["A2"].Value = 6;
///ws.Cells["A3"].Value = 10;
///
///ws.Cells["C1"].Formula = "=A1+A2";
///ws.Cells["C2"].Formula = "=$A$1-A3";
///ws.Cells["C3"].Formula = "=COUNT(A1:A3)";
///ws.Cells["C4"].Formula = "=AVERAGE($A$1:$A$3)";
///ws.Cells["C5"].Formula = "=SUM(A1:A3,2,3)";
///ws.Cells["C7"].Formula = "= 123 - (-(-(23.5)))";
///
///ws.NamedRanges.Add("DataRange", ws.Cells.GetSubrange("A1", "A3"));
///ws.Cells["C8"].Formula = "=MAX(DataRange)";
///
///CellRange cr = ws.Cells.GetSubrange("B9", "C10");
///cr.Merged = true;
///cr.Formula = "=A1*25";
///
///
///NamedRangeCollection.Add
public override string Formula
{
get
{
CellFormula formula1 = this.ValueInternal as CellFormula;
if (formula1 != null)
{
return formula1.Formula;
}
return null;
}
set
{
this.ValueInternal = new CellFormula(value, base.Parent);
}
}
internal CellFormula FormulaInternal
{
get
{
object obj1 = this.ValueInternal;
if (obj1 is CellFormula)
{
return (CellFormula) obj1;
}
return null;
}
set
{
if (value != null)
{
this.ValueInternal = value;
}
else
{
this.ValueInternal = this.Value;
}
}
}
///
///Returns true if style is default; otherwise, false.
///
public override bool IsStyleDefault
{
get
{
if (this.style != null)
{
return this.style.IsDefault;
}
return true;
}
}
///
///Returns associated merged range if the cell is merged; otherwise, null.
///
///CellRange.Merged
public CellRange MergedRange
{
get
{
if (this.cellValue is MergedCellRange)
{
return (MergedCellRange) this.cellValue;
}
return null;
}
}
///
///Gets or sets cell style (CellStyle) of this cell or
///of merged range if this cell is merged.
///
///
///Unset style properties will be inherited from corresponding row or column. See
///ExcelFile.RowColumnResolutionMethod
///for more details.
///
///CellRange.Merged
///
///ExcelFile.RowColumnResolutionMethod
public override CellStyle Style
{
get
{
if (this.cellValue is MergedCellRange)
{
return ((MergedCellRange) this.cellValue).Style;
}
if (this.style == null)
{
this.style = new CellStyle(base.Parent.ParentExcelFile.CellStyleCache);
}
return this.style;
}
set
{
if (this.cellValue is MergedCellRange)
{
((MergedCellRange) this.cellValue).Style = value;
}
else
{
this.style = new CellStyle(value, base.Parent.ParentExcelFile.CellStyleCache);
}
}
}
///
///Gets or sets value of this cell or of merged range if this cell is merged.
///
///
///Exception is thrown if value for the set is not of supported type (See
///ExcelFile.SupportsType for details).
///Note that the fact some type is supported doesn't mean it is written to Excel file in the native format. As
///Microsoft Excel has just few basic types, the object of supported type will be converted to a similar excel type.
///If similar excel type doesn't exist, value is written as a string value.
///If the value of this property is of DateTime type and
///Style number format is not set, ISO date/time
///format will be used as CellStyle.NumberFormat
///value.
///
///Thrown for unsupported types.
///CellRange.Merged
///
///ExcelFile.SupportsType
///CellStyle.NumberFormat
public override object Value
{
get
{
object obj1 = this.ValueInternal;
if (obj1 is CellFormula)
{
obj1 = ((CellFormula) obj1).Value;
}
return obj1;
}
set
{
if (value != null)
{
ExcelFile.ThrowExceptionForUnsupportedType(value.GetType());
}
object obj1 = this.ValueInternal;
if (obj1 is CellFormula)
{
((CellFormula) obj1).Value = obj1;
}
else
{
this.ValueInternal = value;
base.CheckMultiline(value);
}
}
}
internal object ValueInternal
{
get
{
if (this.cellValue is MergedCellRange)
{
return ((MergedCellRange) this.cellValue).ValueInternal;
}
return this.cellValue;
}
set
{
if (this.cellValue is MergedCellRange)
{
((MergedCellRange) this.cellValue).ValueInternal = value;
}
else
{
this.cellValue = value;
}
}
}
// Fields
private object cellValue;
private CellStyle style;
}
}