Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Worksheet class

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

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

[ChildElementInfoAttribute(typeof(SheetCalculationProperties))]
[ChildElementInfoAttribute(typeof(WorksheetExtensionList))]
[ChildElementInfoAttribute(typeof(SheetProperties))]
[ChildElementInfoAttribute(typeof(SheetDimension))]
[ChildElementInfoAttribute(typeof(SheetViews))]
[ChildElementInfoAttribute(typeof(SheetFormatProperties))]
[ChildElementInfoAttribute(typeof(Columns))]
[ChildElementInfoAttribute(typeof(SheetData))]
[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(Hyperlinks))]
[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(Drawing))]
[ChildElementInfoAttribute(typeof(LegacyDrawing))]
[ChildElementInfoAttribute(typeof(LegacyDrawingHeaderFooter))]
[ChildElementInfoAttribute(typeof(DrawingHeaderFooter))]
[ChildElementInfoAttribute(typeof(Picture))]
[ChildElementInfoAttribute(typeof(OleObjects))]
[ChildElementInfoAttribute(typeof(Controls))]
[ChildElementInfoAttribute(typeof(WebPublishItems))]
[ChildElementInfoAttribute(typeof(TableParts))]
public class Worksheet : OpenXmlPartRootElement

[ISO/IEC 29500-1 1st Edition]

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.

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft