SpreadsheetDocument Class

Defines SpreadsheetDocument - an OpenXmlPackage represents a Spreadsheet document.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer
    DocumentFormat.OpenXml.Packaging.OpenXmlPackage
      DocumentFormat.OpenXml.Packaging.SpreadsheetDocument

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

Syntax

'Declaration
Public Class SpreadsheetDocument _
    Inherits OpenXmlPackage
'Usage
Dim instance As SpreadsheetDocument
public class SpreadsheetDocument : OpenXmlPackage

Examples

The following example creates a basic Excel document, a workbook with one worksheet. After you run the code example, take a look at the created file, “SpreadsheetDocumentEx.xlsx,” and notice the worksheet named "mySheet.”

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

namespace SpreadsheetDocumentEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"c:\Users\Public\Documents\SpreadsheetDocumentEx.xlsx";

            // Create a spreadsheet document by supplying the file name.
            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);

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

            Console.WriteLine("The spreadsheet document has been created.\nPress a key.");
            Console.ReadKey();
        }
    }
}
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Spreadsheet
Module Module1
    Sub Main()
        ' Create a spreadsheet document by supplying the file name.
        Dim fileName As String = "c:\Users\Public\Documents\SpreadsheetDocumentEx.xlsx"
        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 Sheet = New Sheet
        sheet.Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart)
        sheet.SheetId = 1
        sheet.Name = "mySheet"
        sheets.Append(sheet)
        workbookpart.Workbook.Save()

        ' Close the document.
        spreadsheetDocument.Close()

        Console.WriteLine("The spreadsheet document has been created." +
                          ControlChars.Lf + "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

SpreadsheetDocument Members

DocumentFormat.OpenXml.Packaging Namespace