Cell Class

Cell.When the object is serialized out as xml, its qualified name is x:c.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
      DocumentFormat.OpenXml.Spreadsheet.CellType
        DocumentFormat.OpenXml.Spreadsheet.Cell

Namespace:  DocumentFormat.OpenXml.Spreadsheet
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

Syntax

'Declaration
Public Class Cell _
    Inherits CellType
'Usage
Dim instance As Cell
public class Cell : CellType

Remarks

The following table lists the possible child types:

  • CellFormula <x:f>

  • CellValue <x:v>

  • InlineString <x:is>

  • ExtensionList <x:extLst>

[ISO/IEC 29500-1 1st Edition]

18.3.1.4 c (Cell)

This collection represents a cell in the worksheet. Information about the cell's location (reference), value, data type, formatting, and formula is expressed here.

[Example:This example shows the information stored for a cell whose address in the grid is C6, whose style index is '6', and whose value metadata index is '15'. The cell contains a formula as well as a calculated result of that formula.

<c r="C6" s="1" vm="15">
<f>CUBEVALUE("xlextdat9 Adventure Works",C$5,$A6)</f>
<v>2838512.355</v>
</c>

end example]

While a cell can have a formula element f and a value element v, when the cell's type t is inlineStr then only the element is is allowed as a child element.

[Example:

Here is an example of expressing a string in the cell rather than using the shared string table.

<row r="1" spans="1:1">
<c r="A1" t="inlineStr">
<is><t>This is inline string example</t></is>
</c>
</row>

end example]

Parent Elements

row (§18.3.1.73)

Child Elements

Subclause

extLst (Future Feature Data Storage Area)

§18.2.10

f (Formula)

§18.3.1.40

is (Rich Text Inline)

§18.3.1.53

v (Cell Value)

§18.3.1.96

Attributes

Description

cm (Cell Metadata Index)

The zero-based index of the cell metadata record associated with this cell. Metadata information is found in the Metadata Part. Cell metadata is extra information stored at the cell level, and is attached to the cell (travels through moves, copy / paste, clear, etc). Cell metadata is not accessible via formula reference.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.

ph (Show Phonetic)

A Boolean value indicating if the spreadsheet application should show phonetic information. Phonetic information is displayed in the same cell across the top of the cell and serves as a 'hint' which indicates how the text should be pronounced. This should only be used for East Asian languages.

The possible values for this attribute are defined by the W3C XML Schema boolean datatype.

r (Reference)

An A1 style reference to the location of this cell

The possible values for this attribute are defined by the ST_CellRef simple type (§18.18.7).

s (Style Index)

The index of this cell's style. Style records are stored in the Styles Part.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.

t (Cell Data Type)

An enumeration representing the cell's data type.

The possible values for this attribute are defined by the ST_CellType simple type (§18.18.11).

vm (Value Metadata Index)

The zero-based index of the value metadata record associated with this cell's value. Metadata records are stored in the Metadata Part. Value metadata is extra information stored at the cell level, but associated with the value rather than the cell itself. Value metadata is accessible via formula reference.

The possible values for this attribute are defined by the W3C XML Schema unsignedInt datatype.

[Note: The W3C XML Schema definition of this element’s content model (CT_Cell) is located in §A.2. end note]

© ISO/IEC29500: 2008.

Examples

The following code example creates a new workbook, inserts a worksheet into it, and writes the word “Microsoft” in a specified cell address.

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

namespace CellEx
{
    class Program
    {
        static void Main(string[] args)
        {
            // Create a spreadsheet document by providing a file name.
            string fileName = @"C:\Users\Public\Documents\myCellEx.xlsx";

            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(fileName, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {   Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1, Name = "mySheet"
            };
            sheets.Append(sheet);
            Worksheet worksheet = new Worksheet();
            SheetData sheetData = new SheetData();
            Row row = new Row();
            Cell cell = new Cell() 
                { CellReference = "A1", DataType = CellValues.String,
                    CellValue = new CellValue("Microsoft") };
            row.Append(cell);
            sheetData.Append(row);
            worksheet.Append(sheetData);
            worksheetPart.Worksheet = worksheet;

            // Close the document.
            spreadsheetDocument.Close();
            Console.WriteLine("All done. Press any key.");
            Console.ReadKey();
        }
    }     
}
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Module Module1
    Sub Main()
        ' Create a spreadsheet document by providing a file name.
        Dim fileName As String = "C:\Users\Public\Documents\myCellEx.xlsx"

        Dim spreadsheetDocument As SpreadsheetDocument = _
            spreadsheetDocument.Create(fileName, SpreadsheetDocumentType.Workbook)

        ' Add a WorkbookPart to the document.
        Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart()
        workbookpart.Workbook = New Workbook()

        ' Add a WorksheetPart to the WorkbookPart.
        Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
        worksheetPart.Worksheet = New Worksheet(New SheetData())

        ' Add Sheets to the Workbook.
        Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook _
        .AppendChild(Of Sheets)(New Sheets())

        ' Append a new worksheet and associate it with the workbook.
        Dim sheet As New Sheet() With { _
         .Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), _
         .SheetId = 1, .Name = "mySheet"}
        sheets.Append(Sheet)
        Dim worksheet As New Worksheet()
        Dim sheetData As New SheetData()
        Dim row As New Row()
        Dim cell As New Cell() With {.CellReference = "A1", .DataType = CellValues.[String], _
         .CellValue = New CellValue("Microsoft")}
        row.Append(Cell)
        sheetData.Append(row)
        worksheet.Append(sheetData)
        worksheetPart.Worksheet = worksheet

        ' Close the document.
        spreadsheetDocument.Close()
        Console.WriteLine("All done. Press any key.")
        Console.ReadKey()
    End Sub
End Module

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

Cell Members

DocumentFormat.OpenXml.Spreadsheet Namespace