Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Sheet Class

Office 2010

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

Namespace:  DocumentFormat.OpenXml.Spreadsheet
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)
public class Sheet : OpenXmlLeafElement

[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.

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();
            }
        }
    }
}

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. All rights reserved.