Worksheet Class
Worksheet. It is the root element of WorksheetPart.When the object is serialized out as xml, its qualified name is x:worksheet.
DocumentFormat.OpenXml.OpenXmlElement
DocumentFormat.OpenXml.OpenXmlCompositeElement
DocumentFormat.OpenXml.OpenXmlPartRootElement
DocumentFormat.OpenXml.Spreadsheet.Worksheet
Assembly: DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)
[ChildElementInfoAttribute(typeof(WorksheetExtensionList))] [ChildElementInfoAttribute(typeof(Hyperlinks))] [ChildElementInfoAttribute(typeof(TableParts))] [ChildElementInfoAttribute(typeof(SheetDimension))] [ChildElementInfoAttribute(typeof(SheetViews))] [ChildElementInfoAttribute(typeof(SheetFormatProperties))] [ChildElementInfoAttribute(typeof(Columns))] [ChildElementInfoAttribute(typeof(SheetData))] [ChildElementInfoAttribute(typeof(SheetCalculationProperties))] [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(SheetProperties))] [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(SmartTags))] [ChildElementInfoAttribute(typeof(Drawing))] [ChildElementInfoAttribute(typeof(LegacyDrawing))] [ChildElementInfoAttribute(typeof(LegacyDrawingHeaderFooter))] [ChildElementInfoAttribute(typeof(DrawingHeaderFooter), FileFormatVersions.Office2010)] [ChildElementInfoAttribute(typeof(Picture))] [ChildElementInfoAttribute(typeof(OleObjects))] [ChildElementInfoAttribute(typeof(Controls))] [ChildElementInfoAttribute(typeof(WebPublishItems))] public class Worksheet : OpenXmlPartRootElement
The following table lists the possible child types:
-
SheetProperties <x:sheetPr>
-
SheetDimension <x:dimension>
-
SheetViews <x:sheetViews>
-
SheetFormatProperties <x:sheetFormatPr>
-
Columns <x:cols>
-
SheetData <x:sheetData>
-
SheetCalculationProperties <x:sheetCalcPr>
-
SheetProtection <x:sheetProtection>
-
ProtectedRanges <x:protectedRanges>
-
Scenarios <x:scenarios>
-
AutoFilter <x:autoFilter>
-
SortState <x:sortState>
-
DataConsolidate <x:dataConsolidate>
-
CustomSheetViews <x:customSheetViews>
-
MergeCells <x:mergeCells>
-
PhoneticProperties <x:phoneticPr>
-
ConditionalFormatting <x:conditionalFormatting>
-
DataValidations <x:dataValidations>
-
Hyperlinks <x:hyperlinks>
-
PrintOptions <x:printOptions>
-
PageMargins <x:pageMargins>
-
PageSetup <x:pageSetup>
-
HeaderFooter <x:headerFooter>
-
RowBreaks <x:rowBreaks>
-
ColumnBreaks <x:colBreaks>
-
CustomProperties <x:customProperties>
-
CellWatches <x:cellWatches>
-
IgnoredErrors <x:ignoredErrors>
-
SmartTags <x:smartTags>
-
Drawing <x:drawing>
-
LegacyDrawing <x:legacyDrawing>
-
LegacyDrawingHeaderFooter <x:legacyDrawingHF>
-
DrawingHeaderFooter <x:drawingHF>
-
Picture <x:picture>
-
OleObjects <x:oleObjects>
-
Controls <x:controls>
-
WebPublishItems <x:webPublishItems>
-
TableParts <x:tableParts>
-
WorksheetExtensionList <x:extLst>
[ISO/IEC 29500-1 1st Edition]
18.3.1.99 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(); } } }