# 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](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.ICellExporter.html). 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](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.ICellImporter.html).

## Using ICellExporter

You can create a concrete instance of `ICellExporter` through the `CreateCellExporter()` method of [IRowExporter](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/row). **Example 1** demonstrates how to add a cell to a row.

**Example 1: Create an ICellExporter for the current row**

```C#
using (ICellExporter cell = row.CreateCellExporter())
{
}
```

> `ICellExporter` inherits from [IDisposable](https://learn.microsoft.com/en-us/dotnet/api/system.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:

* `string`
* `double`
* `bool`
* `DateTime`

> 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: Write a numeric value to a cell**

```C#
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: Assign a SUM formula to a cell**

```C#
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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/features/autofilter#filtering-with-automatic-row-hiding)). 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**

```C#
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");
}
```

> important The `cachedValue` argument 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 an `ArgumentException`.

### 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`](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/row) 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 five cells and write a vertically centered value to the sixth cell**

```C#
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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/worksheet).

**Example 6: Merge the D4:K11 cell range in a worksheet**

```C#
using (IWorksheetExporter worksheet = workbook.CreateWorksheetExporter("Sheet 1"))
{
	worksheet.MergeCells(3, 3, 10, 10);
}
```

> important 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`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadCellFormat.html). The following table describes the properties exposed by `SpreadCellFormat`:

| Property                | Description                                                                                                                                                                                                                                                                                                                           |
| ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `NumberFormat`          | Gets or sets the number format. The format code is interpreted according to the current culture.                                                                                                                                                                                                                                      |
| `InvariantNumberFormat` | Gets or sets a number format using invariant Excel syntax, independently of the current culture. For more information, see [Invariant Number Formats](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/features/culture-spesific-predefined-formats#invariant-number-formats). |
| `HorizontalAlignment`   | Gets or sets the horizontal alignment. Of type [`SpreadHorizontalAlignment`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadHorizontalAlignment.html).                                                                                                                        |
| `VerticalAlignment`     | Gets or sets the vertical alignment. Of type [`SpreadVerticalAlignment`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadVerticalAlignment.html).                                                                                                                              |
| `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`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadThemableColor.html).                                                                                                                                              |
| `FontFamily`            | Gets or sets the font family. Of type [`SpreadThemableFontFamily`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadThemableFontFamily.html).                                                                                                                                   |
| `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`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadUnderlineType.html).                                                                                                                                          |
| `LeftBorder`            | Gets or sets the left border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                           |
| `RightBorder`           | Gets or sets the right border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                          |
| `TopBorder`             | Gets or sets the top border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                            |
| `BottomBorder`          | Gets or sets the bottom border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                         |
| `DiagonalUpBorder`      | Gets or sets the diagonal up border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                    |
| `DiagonalDownBorder`    | Gets or sets the diagonal down border. Of type [`SpreadBorder`](https://docs.telerik.com/devtools/document-processing/api/Telerik.Documents.SpreadsheetStreaming.SpreadBorder.html).                                                                                                                                                  |

**Example 7: Apply borders, fill, font, and alignment settings with SpreadCellFormat**

```C#
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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/features/cell-styles) topic.

**Example 8: Apply string, date, and number formats to exported cell values**

```C#
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](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/row). **Example 9** demonstrates how to read the cells of a row.

**Example 9: Iterate imported cells and read each value, format, and style**

```C#
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`](https://docs.telerik.com/devtools/document-processing/api/telerik.documents.spreadsheetstreaming.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`](https://docs.telerik.com/devtools/document-processing/api/telerik.documents.spreadsheetstreaming.cellvaluetype).               |

## See Also

* [Rows](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/row)
* [Columns](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/column)
* [Worksheets](https://www.telerik.com/document-processing-libraries/documentation/libraries/radspreadstreamprocessing/model/worksheet)
