Export (0) Print
Expand All

CellValue class

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

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

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">
  <v>360</v>
</c>
<c r="C4" t="d">
  <v>1976-11-22T08:30Z</v>
</c>
 

end example]

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

Parent Elements

c (§18.3.1.4); cell (§18.14.1); nc (§18.11.1.3); oc (§18.11.1.5); 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.
                    GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };

            sheets.Append(sheet);
            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")
            };
            
            row.Append(cell);
            sheetData.Append(row);
            worksheet.Append(sheetData);
            worksheetPart.Worksheet = worksheet;
            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();

            Console.WriteLine("All done.\nPress a key.");
            Console.ReadKey();
        }
    }
}

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

Community Additions

ADD
Show:
© 2014 Microsoft