Cells
A cell is the basic data unit in a worksheet. The following sections describe how to export and import cells using RadSpreadStreamProcessing.
What Is a Cell
A cell is the basic data unit in a worksheet. Cells are organized in rows and columns and can also be referred to as an intersection point of a column and a row. Cells are identified by a letter and number combination that indicates the letter of their column and the number of their row. For example, the top left cell is referred to as A1 and the bottom right cell is XFD1048576.
ICellExporter and ICellImporter Interface
In RadSpreadStreamProcessing, a cell can be exported through the ICellExporter interface. It defines several methods that allow you to set different values and formats to a cell.
If you need to read the cell data and its properties, use the ICellImporter interface.
Using ICellExporter
You can create a concrete instance of ICellExporter through the CreateCellExporter() method of IRowExporter. Example 1 demonstrates how to add a cell to a row.
Example 1: Using ICellExporter
using (ICellExporter cell = row.CreateCellExporter())
{
}
ICellExporterinherits from IDisposable. Ensure the object is disposed when you are done with it. Otherwise, the content will not be written in the exported file. The best way to ensure this is handled properly is to wrap it in a using statement.
Set a Value
With ICellExporter you can set different values to a cell and modify its format.
The SetValue() method exposes several overloads that allow you to set values from the following types:
stringdoubleboolDateTime
To visualize a value as a date or time, you need to set an appropriate number format of the cell. Otherwise, it is treated as a number.
Example 2: Setting a Value to a Cell
using (ICellExporter cell = row.CreateCellExporter())
{
cell.SetValue(123.456);
}
Set a Formula
To allow you to set a formula as a value of a cell, ICellExporter defines the SetFormula() method. This method accepts a string that represents the formula as a parameter. Example 3 shows how to use it.
Example 3: Setting a Formula to a Cell
using (ICellExporter cell = row.CreateCellExporter())
{
cell.SetFormula("=Sum(A1, B2)");
}
All formulas must be set in
InvariantCulture. For example, the decimal separator must be ".", and the list separator must be ",".
Set a Formula with a Cached Value
An overload of SetFormula accepts a second cachedValue string argument. The cached value is the pre-computed result of the formula and is used by RadSpreadStreamProcessing for filter evaluation when you export an AutoFilter with automatic row hiding (see AutoFilter). Providing a cached value lets the library evaluate whether a row matches the filter rules without recalculating formulas.
Example 4: Setting a Formula with a Cached Value
using (ICellExporter cellExporter = rowExporter.CreateCellExporter())
{
// Store the formula and its pre-computed result so that the AutoFilter
// can evaluate whether this row matches the filter criteria.
cellExporter.SetFormula("SUM(B2:B10)", "12500");
}
The
cachedValueargument must not contain XML 1.0 illegal control characters (U+0000–U+0008, U+000B, U+000C, U+000E–U+001F). Passing a value with such characters throws anArgumentException.
Skip Cells
The cells in a document are exported one by one from left to right starting from the one with index [0, 0] or, in other words, A1. To export a cell with a bigger index, you need to export all the previous cells or skip them.
In some cases you may need to skip several cells and start filling the data in the next one. The IRowExporter interface declares a method that allows you to implement such a scenario. Example 5 shows how to skip 5 cells and set a value and a vertical alignment to the sixth one.
Example 5: Skip Cells
row.SkipCells(5);
using (ICellExporter cell = row.CreateCellExporter())
{
cell.SetValue("Aligned cell");
cell.SetFormat(new SpreadCellFormat()
{
VerticalAlignment = SpreadVerticalAlignment.Center
});
}
Merge Cells
Example 6 shows how to merge several cells into a single one through IWorksheetExporter.
Example 6: Merge Cells
using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet 1"))
{
worksheet.MergeCells(3, 3, 10, 10);
}
Due to the importance of the order the content is inserted in a document, the Merge operation must be the last operation before disposing
IWorksheetExporter.
The merged cell range has the formatting and value of the top left cell of the range.
Set a Format
Another method exposed by ICellExporter—SetFormat()—enables you to change the appearance of a cell. The SetFormat() method accepts an argument of type SpreadCellFormat. The following table describes the properties exposed by SpreadCellFormat:
| Property | Description |
|---|---|
NumberFormat | Gets or sets the number format. |
HorizontalAlignment | Gets or sets the horizontal alignment. Of type SpreadHorizontalAlignment. |
VerticalAlignment | Gets or sets the vertical alignment. Of type SpreadVerticalAlignment. |
Indent | Gets or sets the indent. |
WrapText | Gets or sets a value indicating whether the text in a cell is line-wrapped within the cell. |
ForeColor | Gets or sets the fore color. Of type SpreadThemableColor. |
FontFamily | Gets or sets the font family. Of type SpreadThemableFontFamily. |
FontSize | Gets or sets the size of the font. |
IsBold | Gets or sets a value indicating whether the text is bold. |
IsItalic | Gets or sets a value indicating whether the text is italic. |
Underline | Gets or sets the underline type. Of type SpreadUnderlineType. |
LeftBorder | Gets or sets the left border. Of type SpreadBorder. |
RightBorder | Gets or sets the right border. Of type SpreadBorder. |
TopBorder | Gets or sets the top border. Of type SpreadBorder. |
BottomBorder | Gets or sets the bottom border. Of type SpreadBorder. |
DiagonalUpBorder | Gets or sets the diagonal up border. Of type SpreadBorder. |
DiagonalDownBorder | Gets or sets the diagonal down border. Of type SpreadBorder. |
Example 7: Format Cells
SpreadBorder border = new SpreadBorder(SpreadBorderStyle.Thick, new SpreadThemableColor(new SpreadColor(255, 0, 0)));
SpreadCellFormat cellFormat = new SpreadCellFormat()
{
TopBorder = border,
BottomBorder = border,
DiagonalDownBorder = border,
DiagonalUpBorder = border,
LeftBorder = border,
RightBorder = border,
Fill = SpreadPatternFill.CreateSolidFill(new SpreadColor(255, 0, 0)),
FontFamily = new SpreadThemableFontFamily(SpreadThemeFontType.Major),
FontSize = 22,
ForeColor = new SpreadThemableColor(new SpreadColor(0, 255, 0)),
Underline = SpreadUnderlineType.DoubleAccounting,
IsBold = true,
IsItalic = true,
HorizontalAlignment = SpreadHorizontalAlignment.Fill,
Indent = 5,
VerticalAlignment = SpreadVerticalAlignment.Top,
WrapText = false
};
cell.SetFormat(cellFormat);
In addition to the listed properties, the SpreadCellFormat class allows you to set a style to a cell. For more information on cell styles, see the Cell Styles topic.
Example 8: Set the Value Format to String, Date, or a Number
using (ICellExporter cell = row.CreateCellExporter())
{
SpreadCellFormat format = new SpreadCellFormat()
{
NumberFormat = "@",
IsBold = true
};
cell.SetFormat(format);
cell.SetValue("test");
}
using (ICellExporter cell = row.CreateCellExporter())
{
SpreadCellFormat format = new SpreadCellFormat()
{
NumberFormat = "dd/mm/yyyy",
IsBold = true
};
cell.SetFormat(format);
cell.SetValue(DateTime.Now.ToOADate());
}
using (ICellExporter cell = row.CreateCellExporter())
{
SpreadCellFormat format = new SpreadCellFormat()
{
NumberFormat = "#,##0.00",
IsBold = true
};
cell.SetFormat(format);
cell.SetValue(42370.12);
}
You can apply a SpreadCellFormat instance on multiple cells. However, if a property of the format changes, the new settings apply to the cells formatted after the modification.
Read a Cell
Using ICellImporter
You can get a concrete instance of ICellImporter through the Cells collection of IRowImporter. Example 9 demonstrates how to read the cells of a row.
Example 9: Create ICellImporter
foreach (ICellImporter cell in rowImporter.Cells)
{
string value = cell.Value;
SpreadCellFormat format = cell.Format;
SpreadCellStyle style = cell.Format.CellStyle;
}
The ICellImporter interface exposes the following properties:
| Property | Description |
|---|---|
RowIndex | Gets the index of the row the cell appears in. |
ColumnIndex | Gets the index of the column the cell appears in. |
Format | Gets the formatting applied to the cell. Of type SpreadCellFormat. |
Value | A string property that allows you to get the value of the cell. |
ValueType | Gets the value type of the cell. Of type CellValueType. |