CellValue Class

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

Inheritance Hierarchy


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


Public Class CellValue _
    Inherits XstringType
Dim instance As CellValue
public class CellValue : XstringType


[ISO/IEC 29500-1 1st Edition] v (Cell Value)

This element expresses the value contained in a cell. If the cell contains a string, then this value is an index into the shared string table, pointing to the actual string value. Otherwise, the value of the cell is expressed directly in this element. Cells containing formulas express the last calculated result of the formula in this element.

For applications not wanting to implement the shared string table, an 'inline string' can be expressed in an <is> element under <c> (instead of a <v> element under <c>),in the same way a string would be expressed in the shared string table. [Note: See <is> for an example. end note]

[Example:In this example cell B4 contains the number "360" and C4 contains the UTC date 22 November 1976, 08:30.

<c r="B4">
<c r="C4" t="d">

end example]

The possible values for this element are defined by the ST_Xstring simple type (§

Parent Elements

c (§; cell (§18.14.1); nc (§; oc (§; tp (§18.15.3)

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

© ISO/IEC29500: 2008.


The following code example creates a spreadsheet file named "CellValue.xlsx" and writes the text "Microsoft" in the cell A2.

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

namespace CellTypeEx
    class Program
        static void Main(string[] args)
            string fileName = @"C:\Users\Public\Documents\CellValueEx.xlsx";

            // Create a spreadsheet document by supplying the fileName.
            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.
                SheetId = 1,
                Name = "mySheet"

            Worksheet worksheet = new Worksheet();
            SheetData sheetData = new SheetData();

            Row row = new Row() { RowIndex = 2U, Spans = new ListValue<StringValue>() };
            Cell cell = new Cell()
                CellReference = "A2",
                DataType = CellValues.String,
                CellValue = new CellValue("Microsoft")
            worksheetPart.Worksheet = worksheet;

            // Close the document.

            Console.WriteLine("All done.\nPress a key.");
Imports System
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Module Module1
    Sub Main(ByVal args As String())
        Dim fileName As String = "C:\Users\Public\Documents\CellValueEx.xlsx"

        ' Create a spreadsheet document by supplying the fileName.
        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" _

        Dim worksheet As New Worksheet()
        Dim sheetData As New SheetData()

        Dim row As New Row() With { _
         .RowIndex = 2UI, _
         .Spans = New ListValue(Of StringValue)() _
        Dim cell As New Cell() With { _
         .CellReference = "A2", _
         .DataType = CellValues.[String], _
         .CellValue = New CellValue("Microsoft") _

        worksheetPart.Worksheet = worksheet

        ' Close the document.

        Console.WriteLine("All done." & vbLf & "Press a key.")
    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


CellValue Members

DocumentFormat.OpenXml.Spreadsheet Namespace