WorksheetPart Class

Defines the WorksheetPart.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer
    DocumentFormat.OpenXml.Packaging.OpenXmlPart
      DocumentFormat.OpenXml.Packaging.WorksheetPart

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

Syntax

'Declaration
Public Class WorksheetPart _
    Inherits OpenXmlPart _
    Implements IFixedContentTypePart
'Usage
Dim instance As WorksheetPart
public class WorksheetPart : OpenXmlPart, 
    IFixedContentTypePart

Examples

The following code example opens a zip file and creates a spreadsheet document into it.

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

namespace WorkSheetPartEx
{
    class Program
    {
        // Create a spreadsheet by Package.
        static void Main(string[] args)
        {
            string fileName = @"C:\users\public\documents\WorkSheetPartEx.zip";
 
            // Open a package file. 
            Package package = Package.Open(fileName);

            // Create the spreadsheet document.
            SpreadsheetDocument spreadsheetDocument = 
                SpreadsheetDocument.Create(package, SpreadsheetDocumentType.Template);

            // Add a WorkbookPart.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart.
            WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add a sheets list.
            Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook
                .AppendChild<Sheets>(new Sheets());

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart
                .GetIdOfPart(worksheetPart), SheetId = 1, Name = "mySheet" };
            sheets.Append(sheet);

            // Close the spreadsheet and the package.
            spreadsheetDocument.Close();
            package.Close();

            Console.WriteLine("The package has been created.\nPress a key.");
            Console.ReadKey();
        }
    }
}
Imports System.IO.Packaging
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet

Module Module1
    ' Create a spreadsheet by Package.
    Sub Main(ByVal args As String())
        Dim fileName As String = "C:\users\public\documents\WorkSheetPartEx.zip"

        ' Open a package file. 
        Dim package As Package = package.Open(fileName)

        ' Create the spreadsheet document.
        Dim spreadsheetDocument As SpreadsheetDocument = spreadsheetDocument.Create(package, SpreadsheetDocumentType.Template)

        ' Add a WorkbookPart.
        Dim workbookpart As WorkbookPart = spreadsheetDocument.AddWorkbookPart()
        workbookpart.Workbook = New Workbook()

        ' Add a WorksheetPart.
        Dim worksheetPart As WorksheetPart = workbookpart.AddNewPart(Of WorksheetPart)()
        worksheetPart.Worksheet = New Worksheet(New SheetData())

        ' Add a sheets list.
        Dim sheets As Sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(Of Sheets)(New Sheets())

        ' Append the 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)

        ' Close the spreadsheet and the package.
        spreadsheetDocument.Close()
        package.Close()

        Console.WriteLine("The package has been created." & vbLf & "Press a key.")
        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

WorksheetPart Members

DocumentFormat.OpenXml.Packaging Namespace