Поделиться через


Worksheet Class

Worksheet. It is the root element of WorksheetPart.When the object is serialized out as xml, its qualified name is x:worksheet.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
      DocumentFormat.OpenXml.OpenXmlPartRootElement
        DocumentFormat.OpenXml.Spreadsheet.Worksheet

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

Syntax

'Declaration
<ChildElementInfoAttribute(GetType(WorksheetExtensionList))> _
<ChildElementInfoAttribute(GetType(Hyperlinks))> _
<ChildElementInfoAttribute(GetType(TableParts))> _
<ChildElementInfoAttribute(GetType(SheetDimension))> _
<ChildElementInfoAttribute(GetType(SheetViews))> _
<ChildElementInfoAttribute(GetType(SheetFormatProperties))> _
<ChildElementInfoAttribute(GetType(Columns))> _
<ChildElementInfoAttribute(GetType(SheetData))> _
<ChildElementInfoAttribute(GetType(SheetCalculationProperties))> _
<ChildElementInfoAttribute(GetType(SheetProtection))> _
<ChildElementInfoAttribute(GetType(ProtectedRanges))> _
<ChildElementInfoAttribute(GetType(Scenarios))> _
<ChildElementInfoAttribute(GetType(AutoFilter))> _
<ChildElementInfoAttribute(GetType(SortState))> _
<ChildElementInfoAttribute(GetType(DataConsolidate))> _
<ChildElementInfoAttribute(GetType(CustomSheetViews))> _
<ChildElementInfoAttribute(GetType(MergeCells))> _
<ChildElementInfoAttribute(GetType(PhoneticProperties))> _
<ChildElementInfoAttribute(GetType(ConditionalFormatting))> _
<ChildElementInfoAttribute(GetType(DataValidations))> _
<ChildElementInfoAttribute(GetType(SheetProperties))> _
<ChildElementInfoAttribute(GetType(PrintOptions))> _
<ChildElementInfoAttribute(GetType(PageMargins))> _
<ChildElementInfoAttribute(GetType(PageSetup))> _
<ChildElementInfoAttribute(GetType(HeaderFooter))> _
<ChildElementInfoAttribute(GetType(RowBreaks))> _
<ChildElementInfoAttribute(GetType(ColumnBreaks))> _
<ChildElementInfoAttribute(GetType(CustomProperties))> _
<ChildElementInfoAttribute(GetType(CellWatches))> _
<ChildElementInfoAttribute(GetType(IgnoredErrors))> _
<ChildElementInfoAttribute(GetType(SmartTags))> _
<ChildElementInfoAttribute(GetType(Drawing))> _
<ChildElementInfoAttribute(GetType(LegacyDrawing))> _
<ChildElementInfoAttribute(GetType(LegacyDrawingHeaderFooter))> _
<ChildElementInfoAttribute(GetType(DrawingHeaderFooter), FileFormatVersions.Office2010)> _
<ChildElementInfoAttribute(GetType(Picture))> _
<ChildElementInfoAttribute(GetType(OleObjects))> _
<ChildElementInfoAttribute(GetType(Controls))> _
<ChildElementInfoAttribute(GetType(WebPublishItems))> _
Public Class Worksheet _
    Inherits OpenXmlPartRootElement
'Usage
Dim instance As Worksheet
[ChildElementInfoAttribute(typeof(WorksheetExtensionList))]
[ChildElementInfoAttribute(typeof(Hyperlinks))]
[ChildElementInfoAttribute(typeof(TableParts))]
[ChildElementInfoAttribute(typeof(SheetDimension))]
[ChildElementInfoAttribute(typeof(SheetViews))]
[ChildElementInfoAttribute(typeof(SheetFormatProperties))]
[ChildElementInfoAttribute(typeof(Columns))]
[ChildElementInfoAttribute(typeof(SheetData))]
[ChildElementInfoAttribute(typeof(SheetCalculationProperties))]
[ChildElementInfoAttribute(typeof(SheetProtection))]
[ChildElementInfoAttribute(typeof(ProtectedRanges))]
[ChildElementInfoAttribute(typeof(Scenarios))]
[ChildElementInfoAttribute(typeof(AutoFilter))]
[ChildElementInfoAttribute(typeof(SortState))]
[ChildElementInfoAttribute(typeof(DataConsolidate))]
[ChildElementInfoAttribute(typeof(CustomSheetViews))]
[ChildElementInfoAttribute(typeof(MergeCells))]
[ChildElementInfoAttribute(typeof(PhoneticProperties))]
[ChildElementInfoAttribute(typeof(ConditionalFormatting))]
[ChildElementInfoAttribute(typeof(DataValidations))]
[ChildElementInfoAttribute(typeof(SheetProperties))]
[ChildElementInfoAttribute(typeof(PrintOptions))]
[ChildElementInfoAttribute(typeof(PageMargins))]
[ChildElementInfoAttribute(typeof(PageSetup))]
[ChildElementInfoAttribute(typeof(HeaderFooter))]
[ChildElementInfoAttribute(typeof(RowBreaks))]
[ChildElementInfoAttribute(typeof(ColumnBreaks))]
[ChildElementInfoAttribute(typeof(CustomProperties))]
[ChildElementInfoAttribute(typeof(CellWatches))]
[ChildElementInfoAttribute(typeof(IgnoredErrors))]
[ChildElementInfoAttribute(typeof(SmartTags))]
[ChildElementInfoAttribute(typeof(Drawing))]
[ChildElementInfoAttribute(typeof(LegacyDrawing))]
[ChildElementInfoAttribute(typeof(LegacyDrawingHeaderFooter))]
[ChildElementInfoAttribute(typeof(DrawingHeaderFooter), FileFormatVersions.Office2010)]
[ChildElementInfoAttribute(typeof(Picture))]
[ChildElementInfoAttribute(typeof(OleObjects))]
[ChildElementInfoAttribute(typeof(Controls))]
[ChildElementInfoAttribute(typeof(WebPublishItems))]
public class Worksheet : OpenXmlPartRootElement

Remarks

The following table lists the possible child types:

  • SheetProperties <x:sheetPr>

  • SheetDimension <x:dimension>

  • SheetViews <x:sheetViews>

  • SheetFormatProperties <x:sheetFormatPr>

  • Columns <x:cols>

  • SheetData <x:sheetData>

  • SheetCalculationProperties <x:sheetCalcPr>

  • SheetProtection <x:sheetProtection>

  • ProtectedRanges <x:protectedRanges>

  • Scenarios <x:scenarios>

  • AutoFilter <x:autoFilter>

  • SortState <x:sortState>

  • DataConsolidate <x:dataConsolidate>

  • CustomSheetViews <x:customSheetViews>

  • MergeCells <x:mergeCells>

  • PhoneticProperties <x:phoneticPr>

  • ConditionalFormatting <x:conditionalFormatting>

  • DataValidations <x:dataValidations>

  • Hyperlinks <x:hyperlinks>

  • PrintOptions <x:printOptions>

  • PageMargins <x:pageMargins>

  • PageSetup <x:pageSetup>

  • HeaderFooter <x:headerFooter>

  • RowBreaks <x:rowBreaks>

  • ColumnBreaks <x:colBreaks>

  • CustomProperties <x:customProperties>

  • CellWatches <x:cellWatches>

  • IgnoredErrors <x:ignoredErrors>

  • SmartTags <x:smartTags>

  • Drawing <x:drawing>

  • LegacyDrawing <x:legacyDrawing>

  • LegacyDrawingHeaderFooter <x:legacyDrawingHF>

  • DrawingHeaderFooter <x:drawingHF>

  • Picture <x:picture>

  • OleObjects <x:oleObjects>

  • Controls <x:controls>

  • WebPublishItems <x:webPublishItems>

  • TableParts <x:tableParts>

  • WorksheetExtensionList <x:extLst>

[ISO/IEC 29500-1 1st Edition]

18.3.1.99 worksheet (Worksheet)

This is the root element of Worksheet parts within a SpreadsheetML document.

Parent Elements

Root element of SpreadsheetML Worksheet part

Child Elements

Subclause

autoFilter (AutoFilter Settings)

§18.3.1.2

cellWatches (Cell Watch Items)

§18.3.1.9

colBreaks (Vertical Page Breaks)

§18.3.1.14

cols (Column Information)

§18.3.1.17

conditionalFormatting (Conditional Formatting)

§18.3.1.18

controls (Embedded Controls)

§18.3.1.21

customProperties (Custom Properties)

§18.3.1.23

customSheetViews (Custom Sheet Views)

§18.3.1.27

dataConsolidate (Data Consolidate)

§18.3.1.29

dataValidations (Data Validations)

§18.3.1.33

dimension (Worksheet Dimensions)

§18.3.1.35

drawing (Drawing)

§18.3.1.36

drawingHF (Drawing Reference in Header Footer)

§18.3.1.37

extLst (Future Feature Data Storage Area)

§18.2.10

headerFooter (Header Footer Settings)

§18.3.1.46

hyperlinks (Hyperlinks)

§18.3.1.48

ignoredErrors (Ignored Errors)

§18.3.1.51

mergeCells (Merge Cells)

§18.3.1.55

oleObjects (Embedded Objects)

§18.3.1.60

pageMargins (Page Margins)

§18.3.1.62

pageSetup (Page Setup Settings)

§18.3.1.63

phoneticPr (Phonetic Properties)

§18.4.3

picture (Background Image)

§18.3.1.67

printOptions (Print Options)

§18.3.1.70

protectedRanges (Protected Ranges)

§18.3.1.72

rowBreaks (Horizontal Page Breaks (Row))

§18.3.1.74

scenarios (Scenarios)

§18.3.1.76

sheetCalcPr (Sheet Calculation Properties)

§18.3.1.79

sheetData (Sheet Data)

§18.3.1.80

sheetFormatPr (Sheet Format Properties)

§18.3.1.81

sheetPr (Sheet Properties)

§18.3.1.82

sheetProtection (Sheet Protection Options)

§18.3.1.85

sheetViews (Sheet Views)

§18.3.1.88

smartTags (Smart Tags)

§18.3.1.90

sortState (Sort State)

§18.3.1.92

tableParts (Table Parts)

§18.3.1.95

webPublishItems (Web Publishing Items)

§18.3.1.98

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

© ISO/IEC29500: 2008.

Examples

The following code example inserts a new worksheet into a spreadsheet document by providing its file name. After you run the code example, examine the test file “WorksheetEx.xlsx” and notice the new worksheet named “mySheet.”

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

namespace WorkSheetEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"C:\Users\Public\Documents\WorksheetEx.xlsx";
            
            using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(fileName, true))
            {
                // Add a WorksheetPart.
                WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart.Worksheet = new Worksheet(new SheetData());

                // Create Sheets object.
                Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>();
                string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart);

                // Create a unique ID for the new worksheet.
                uint sheetId = 1;
                if (sheets.Elements<Sheet>().Count() > 0)
                {
                    sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                }

                // Give the new worksheet a name.
                string sheetName = "mySheet" + sheetId;

                // Append the new worksheet and associate it with the workbook.
                Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
                sheets.Append(sheet);
            }
            Console.WriteLine("All done.");
            Console.ReadKey();
        }
    }
}
Imports System.Linq
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Module Module1
    Sub Main()
        Dim fileName As String = "C:\Users\Public\Documents\WorksheetEx.xlsx"
        Dim spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, True)

        Using (spreadSheet)
            ' Add a WorksheetPart.
            Dim newWorksheetPart As WorksheetPart = spreadSheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
            newWorksheetPart.Worksheet = New Worksheet(New SheetData())

            ' Create a Sheets object.
            Dim sheets As Sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
            Dim relationshipId As String = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart)

            ' Get a unique ID for the new worksheet.
            Dim sheetId As UInteger = 1
            If (sheets.Elements(Of Sheet).Count > 0) Then
                sheetId = sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1
            End If

            ' Give the new worksheet a name.
            Dim sheetName As String = ("mySheet" + sheetId.ToString())

            ' Append the new worksheet and associate it with the workbook.
            Dim sheet As Sheet = New Sheet
            sheet.Id = relationshipId
            sheet.SheetId = sheetId
            sheet.Name = sheetName
            sheets.Append(sheet)
        End Using
        Console.WriteLine("All done.")
        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

Worksheet Members

DocumentFormat.OpenXml.Spreadsheet Namespace