Class Cell
Represents a Cell in an Excel WorkSheet.
Inheritance
Namespace: IronXL
Assembly: IronXL.dll
Syntax
public class Cell : Object
Cell holds the value, formula, and formatting of one position in an Excel sheet in C#, and exposes the typed readers a developer needs to pull that value out as the right .NET type. Every single-cell read or write in IronXL ends at a Cell, whether the goal is the raw text, a parsed number, a date, or the formula behind a computed result. It is the type behind a search like "C# Excel cell value".
A cell is reached from a worksheet, most often through the indexer, so workSheet["A1"] returns the Cell at that address, or through GetCellAt. From there Value reads or assigns the cell's contents as an object, while Text gives the display string and Formula reads or sets a formula like =SUM(A1:A10).
For typed access, the cell offers IntValue, DoubleValue, DecimalValue, DateTimeValue, and BoolValue, which convert the stored value to that type, plus the IsFormula, IsEmpty, IsText, and IsNumeric flags for testing what a cell holds before reading it. Presentation lives on Style for fonts, borders, and fill, and on FormatString for the number or date format. A cell also carries its Address, an attached Hyperlink, and an optional Comment, and Copy duplicates it to another location. Setting Value and setting Formula are alternatives on the same cell, since a value-bearing cell and a formula-bearing one are the same object in two states.
using IronXL;
WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.GetWorkSheet("Sheet1");
Cell cell = workSheet["A1"];
cell.Value = 42;
cell.Style.Font.Bold = true;
workBook.SaveAs("output.xlsx");The select range how-to covers cell access, the set cell data format how-to shows FormatString, and the edit formulas how-to works with Formula.
Properties
Address
The location of this Cell within the WorkSheet
Address of the cell in a WorkSheet represented as a RangeAddress.
Declaration
public RangeAddress Address { get; }
Property Value
| Type | Description |
|---|---|
| RangeAddress |
AddressString
Gets the cell location in Excel Notation. E.G. "A12"
Declaration
public string AddressString { get; }
Property Value
| Type | Description |
|---|---|
| System.String | The cell location. |
BoolValue
Gets or sets the boolean value of a cell.
Declaration
public virtual bool BoolValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
ColumnIndex
Zero-based column index of a column in the sheet that contains this cell.
e.g. if this Cell is in Excel col "A": ColumnIndex will equal 0
Declaration
public int ColumnIndex { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Comment
Gets the comment associated with this cell. Returns null if no comment is associated with the cell.
Declaration
public Comment Comment { get; }
Property Value
| Type | Description |
|---|---|
| Comment |
DateTimeValue
Gets or sets the date time value of this Cell.
Declaration
public Nullable<DateTime> DateTimeValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Nullable<System.DateTime> | The System.DateTime value of this Cell.. |
DecimalValue
Gets or sets the numeric System.Decimal value of the cell. If the cell is non-numeric - zero will be returned.
Declaration
public virtual Decimal DecimalValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Decimal | The System.Decimal value of the cell. |
DoubleValue
Gets or sets the numeric System.Double value of the cell. If the cell is non-numeric - zero will be returned.
Declaration
public virtual double DoubleValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Double | The System.Double value of the cell. |
FloatValue
Gets or sets the numeric System.Single value of the cell. If the cell is non-numeric - zero will be returned.
Declaration
public virtual float FloatValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Single | The System.Single value of the cell. |
FormatString
The Excel Cell format as a string . E.g. $#.00 or 0.00% or m/d/yyyy
Read more about number formats at https://www.ablebits.com/office-addins-blog/2016/07/07/custom-excel-number-format/
Declaration
public string FormatString { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Remarks
You can find the list of the built-in format strings in the class BuiltinFormats.
See Also
FormattedCellValue
Returns the Cell value as a human readable string.
FormattedCellValue applies cell formatting exactly as displayed in Excel, rather than the raw numerical values that underlie the sheet.
Declaration
public string FormattedCellValue { get; }
Property Value
| Type | Description |
|---|---|
| System.String |
Formula
Property to get or set cell's formula.
e.g. =A2+A2+A3+A4
Declaration
public string Formula { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Hyperlink
Property to get or set the cell's value as a hyperlink.
Available link types are:
A URL starting with http:// , https:// or ftp://
An Email starting with mailto:
A file by specifying absolute path starting with file:///
A cell or defined name in the current spreadsheet
Declaration
public string Hyperlink { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Int32Value
Gets or sets the numeric System.Int32 value of the cell. If the cell is non-numeric - zero will be returned.
Synonym of IntValue
Declaration
public virtual int Int32Value { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The System.Int32 value of the cell. |
Int64Value
Gets or sets the numeric System.Int64 value of the cell. If the cell is non-numeric - zero will be returned.
Synonym of LongValue
Declaration
public virtual long Int64Value { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int64 | The System.Int64 value of the cell. |
IntValue
Gets or sets the System.Int32 value of the cell. If the cell is non-numeric - zero will be returned.
Declaration
public virtual int IntValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | The System.Int32 value of the cell. |
IsBoolean
Gets a value indicating whether this instance is boolean.
Declaration
public bool IsBoolean { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
IsDateTime
Boolean property that indicates if cell contains System.DateTime value.
Declaration
public bool IsDateTime { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
IsEmpty
Boolean property that indicates if cell is empty.
Declaration
public bool IsEmpty { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
IsError
Boolean property that indicates if cell has an error in its formula.
Declaration
public bool IsError { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
IsFormula
Boolean property that indicates if cell contains a formula.
Declaration
public bool IsFormula { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
IsNumeric
Boolean property that indicates if this Cell contains a numeric value:
E.g. cell contains a number such as decimal, int, long, float, double etc.
Declaration
public bool IsNumeric { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
IsText
Boolean property that indicates if cell contains a string value.
Declaration
public bool IsText { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
Location
Gets the cell location in Excel Notation. E.G. "A12"
Synonym of AddressString.
Declaration
public string Location { get; }
Property Value
| Type | Description |
|---|---|
| System.String | The cell location. |
LongValue
Gets or sets the numeric System.Int64 value of the cell. If the cell is non-numeric - zero will be returned.
Declaration
public virtual long LongValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Int64 | The System.Int64 value of the cell. |
RowIndex
Zero-based row index of the row in the sheet that contains this cell.
e.g. if this Cell is in Excel row 3: RowIndex will equal 2
Declaration
public int RowIndex { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
StringValue
Gets or sets the string value of this Cell.
Declaration
public virtual string StringValue { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String | The string value of the cell. Numbers and dates will be rendered into readable strings; as displayed in Excel. Error end NAN cells will return as a null string |
Style
The visual Style of this Cell.
Declaration
public IStyle Style { get; }
Property Value
| Type | Description |
|---|---|
| IStyle |
See Also
Text
Gets or sets the string value of this Cell.
Synonym of IronXL.Cells.ICell.StringValue.
Declaration
public string Text { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String | The text as displayed in the cell. |
Value
Property to Get or Set the cell's value.
Cells may be get or set as types: System.Double, System.Decimal, System.Single, System.Int64, System.Int32, System.Boolean, System.DateTime or System.String.
See IsFormula, IsEmpty, IsDateTime, IsNumeric, IsText and TryGetValue<TType>(out TType) for elegantly parsing sheets with unknown content.
When System.DateTime values, the default Excel date display format is 'M/D/YY h:mm'. Use SetDateTimeValue(DateTime, String) to set a custom date display format
Declaration
public object Value { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Object |
See Also
Methods
AddComment(String, String, Boolean)
Adds a comment to the cell.
Declaration
public Comment AddComment(string content, string author, bool isVisible = false)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | content | The content of the comment. |
| System.String | author | The author of the comment. |
| System.Boolean | isVisible | if set to |
Returns
| Type | Description |
|---|---|
| Comment |
Copy(WorkSheet, String)
Copies the specified cell to the sheet and inserts it at the specified point.
Declaration
public void Copy(WorkSheet sheet, string pointToInsert)
Parameters
| Type | Name | Description |
|---|---|---|
| WorkSheet | sheet | The worksheet to insert. |
| System.String | pointToInsert | Point to insert. e.g. "A7". |
Evaluate()
Evaluates the formula, if the cell contains a formula, and saves the result of the formula. The cell remains as a formula cell. Else if the cell does not contain formula, this method leaves the cell unchanged.
Declaration
public Cell Evaluate()
Returns
| Type | Description |
|---|---|
| Cell |
GetValue<TType>()
An method for getting a strongly-typed cell value which may throw cast exceptions if data is not in the System.Type expected.
Throws System.ArgumentException if casting to
TType is impossible.
Use Try/Catch or TryGetValue<TType>(out TType) to cast cell values without possible exceptions.
See Value for the list of valid types.
Declaration
public TType GetValue<TType>()
Returns
| Type | Description |
|---|---|
| TType | The value of the cell, cast as TType. E.g: double, decimal, float, long, int, bool, DateTime, string or object. |
Type Parameters
| Name | Description |
|---|---|
| TType | The type of .net object to cast the result as. E.g: double, decimal, float, long, int, bool, DateTime, string or object. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentException |
RemoveComment()
Removes the comment from the cell. If the cell does not contain a comment, this method does nothing.
Declaration
public void RemoveComment()
RemoveFormula()
Removes the formula from the cell. If the cell does not contain a formula, this method does nothing.
Declaration
public void RemoveFormula()
RemoveHyperlink()
Removes Hyperlink from the cell. If the cell does not contain a Hyperlink, nothing happens.
Declaration
public void RemoveHyperlink()
SetDateTimeValue(DateTime, String)
Method for setting System.DateTime value with custom format.
Declaration
public void SetDateTimeValue(DateTime value, string format)
Parameters
| Type | Name | Description |
|---|---|---|
| System.DateTime | value | DateTime value |
| System.String | format | Custom date format as a string. e.g. M/d/yyyy |
ToString()
Gets the text content of this Cell.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| System.String | A System.String that represents the text content of this Cell. |
TryGetValue<TType>(out TType)
Safe method for getting a strongly-typed cell value.
See Value for the list of valid types.
e.g. bool OK = Cell.TryGetValue<double>(double result);
Declaration
public bool TryGetValue<TType>(out TType value)
Parameters
| Type | Name | Description |
|---|---|---|
| TType | value |
|
Returns
| Type | Description |
|---|---|
| System.Boolean | The value of the cell, cast as TType. E.g: double, decimal, float, long, int, bool, DateTime, string or object. |
Type Parameters
| Name | Description |
|---|---|
| TType | The type of .net object to cast the result as. E.g: double, decimal, float, long, int, bool, DateTime, string or object. |