Compartir a través de


Sheet Class

Sheet Information.When the object is serialized out as xml, its qualified name is x:sheet.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlLeafElement
      DocumentFormat.OpenXml.Spreadsheet.Sheet

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

Syntax

'Declaration
Public Class Sheet _
    Inherits OpenXmlLeafElement
'Usage
Dim instance As Sheet
public class Sheet : OpenXmlLeafElement

Remarks

[ISO/IEC 29500-1 1st Edition]

18.2.19 sheet (Sheet Information)

This element defines a sheet in this workbook. Sheet data is stored in a separate part.

Parent Elements

sheets (§18.2.20)

Attributes

Description

id (Relationship Id)

Namespace: .../officeDocument/2006/relationships

Specifies the identifier of the sheet part where the definition for this sheet is stored.

This attribute is required.

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

name (Sheet Name)

Specifies the name of the sheet. This name shall be unique.

This attribute is required.

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

sheetId (Sheet Tab Id)

Specifies the internal identifier for the sheet. This identifier shall be unique.

This attribute is required.

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

state (Visible State)

Specifies the visible state of this sheet.

The default value for this attribute is "visible."

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

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

© ISO/IEC29500: 2008.

Examples

The following code example inserts a new worksheet in the workbook of an existing spreadsheet document.

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

namespace SheetEx
{
    class Program
    {
        // Insert a new worksheet in a spreadsheet document.
        static void Main(string[] args)
        {
            string document = @"C:\users\public\documents\SheetsEx.xlsx";

            // Open the document for editing.
            using (SpreadsheetDocument spreadsheetDocument
                = SpreadsheetDocument.Open(document, true))
            {
                // Add a blank WorksheetPart.
                WorksheetPart newWorksheetPart = 
                    spreadsheetDocument.WorkbookPart.AddNewPart<WorksheetPart>();
                newWorksheetPart.Worksheet = new Worksheet(new SheetData());

                Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook
                    .GetFirstChild<Sheets>();
                string relationshipId = spreadsheetDocument.WorkbookPart
                    .GetIdOfPart(newWorksheetPart);

                // Get 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("A new sheet has been inserted into the workbook.\nPress a key.");
                Console.ReadKey();
            }
        }
    }
}
Imports System.Linq
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Module Module1
    ' Insert a new worksheet in a spreadsheet document.
    Sub Main(ByVal args As String())
        Dim document As String = "C:\users\public\documents\SheetsEx.xlsx"

        ' Open the document for editing.
        Using spreadsheetDocument As SpreadsheetDocument = _
            spreadsheetDocument.Open(document, True)
            ' Add a blank WorksheetPart.
            Dim newWorksheetPart As WorksheetPart = _
                spreadsheetDocument.WorkbookPart.AddNewPart(Of WorksheetPart)()
            newWorksheetPart.Worksheet = New Worksheet(New SheetData())

            Dim sheets As Sheets = _
                spreadsheetDocument.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
            Dim relationshipId As String = _
                spreadsheetDocument.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

            ' Append the new worksheet and associate it with the workbook.
            Dim sheet As New Sheet() With { _
             .Id = relationshipId, _
             .SheetId = sheetId, _
             .Name = sheetName _
            }
            sheets.Append(sheet)

            Console.WriteLine("A new sheet has been inserted into the workbook." _
                              & vbLf & "Press a key.")
            Console.ReadKey()
        End Using
    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

Sheet Members

DocumentFormat.OpenXml.Spreadsheet Namespace