Partager via


CellType Class

Defines the CellType class.

Inheritance Hierarchy

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

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

Syntax

'Declaration
<ChildElementInfoAttribute(GetType(CellFormula))> _
<ChildElementInfoAttribute(GetType(CellValue))> _
<ChildElementInfoAttribute(GetType(ExtensionList))> _
<ChildElementInfoAttribute(GetType(InlineString))> _
Public MustInherit Class CellType _
    Inherits OpenXmlCompositeElement
'Usage
Dim instance As CellType
[ChildElementInfoAttribute(typeof(CellFormula))]
[ChildElementInfoAttribute(typeof(CellValue))]
[ChildElementInfoAttribute(typeof(ExtensionList))]
[ChildElementInfoAttribute(typeof(InlineString))]
public abstract class CellType : OpenXmlCompositeElement

Remarks

The following table lists the possible child types:CellFormula <x:f>CellValue <x:v>InlineString <x:is>ExtensionList <x:extLst>

Examples

The following example creates a spreadsheet document and adds the text "Microsoft" to the cell A2 by using the CellType class.

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\CellTypeEx.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>() }; 
            CellType cT = new Cell() { CellReference = "A2", DataType = CellValues.String, 
                CellValue = new CellValue("Microsoft") };
            Console.WriteLine(cT.InnerText);

            row.Append(cT);
            sheetData.Append(row);
            worksheet.Append(sheetData);           
            worksheetPart.Worksheet = worksheet;           
            workbookpart.Workbook.Save();

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

            System.Console.WriteLine("All done");
            System.Console.ReadKey();
 
        }
     }
 }
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\CellTypeEx.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" _
        }

        sheets.Append(sheet)
        Dim worksheet As New Worksheet()
        Dim sheetData As New SheetData()

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

        row.Append(cT)
        sheetData.Append(row)
        worksheet.Append(sheetData)
        worksheetPart.Worksheet = worksheet
        workbookpart.Workbook.Save()

        ' Close the document.
        spreadsheetDocument.Close()

        System.Console.WriteLine("All done")
        System.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

CellType Members

DocumentFormat.OpenXml.Spreadsheet Namespace