Skip to content

Commit ab513bd

Browse files
committed
clean up docs\spreadsheet\how-to-insert-a-new-worksheet-into-a-spreadsheet.md
1 parent 1e7f8e1 commit ab513bd

File tree

3 files changed

+62
-25
lines changed

3 files changed

+62
-25
lines changed

docs/spreadsheet/how-to-insert-a-new-worksheet-into-a-spreadsheet.md

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.suite: office
1111
ms.author: o365devx
1212
author: o365devx
1313
ms.topic: conceptual
14-
ms.date: 12/12/2023
14+
ms.date: 01/09/2025
1515
ms.localizationpriority: high
1616
---
1717
# Insert a new worksheet into a spreadsheet document
@@ -20,25 +20,52 @@ This topic shows how to use the classes in the Open XML SDK for
2020
Office to insert a new worksheet into a spreadsheet document
2121
programmatically.
2222

23+
## Getting a SpreadsheetDocument Object
2324

25+
In the Open XML SDK, the <xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument> class represents an
26+
Excel document package. To open and work with an Excel document, you
27+
create an instance of the `SpreadsheetDocument` class from the document.
28+
After you create the instance from the document, you can then obtain
29+
access to the main workbook part that contains the worksheets. The text
30+
in the document is represented in the package as XML using `SpreadsheetML` markup.
31+
32+
To create the class instance from the document that you call one of the
33+
<xref:DocumentFormat.OpenXml.Packaging.SpreadsheetDocument.Open*> methods. Several are provided, each
34+
with a different signature. The sample code in this topic uses the [Open(String, Boolean)](/dotnet/api/documentformat.openxml.packaging.spreadsheetdocument.open?view=openxml-3.0.1#documentformat-openxml-packaging-spreadsheetdocument-open(system-string-system-boolean)) method with a
35+
signature that requires two parameters. The first parameter takes a full
36+
path string that represents the document that you want to open. The
37+
second parameter is either `true` or `false` and represents whether you want the file to
38+
be opened for editing. Any changes that you make to the document will
39+
not be saved if this parameter is `false`.
40+
41+
The code that calls the `Open` method is
42+
shown in the following `using` statement.
43+
44+
### [C#](#tab/cs-1)
45+
[!code-csharp[](../../samples/spreadsheet/insert_a_new_worksheet/cs/Program.cs#snippet1)]
46+
47+
### [Visual Basic](#tab/vb-1)
48+
[!code-vb[](../../samples/spreadsheet/insert_a_new_worksheet/vb/Program.vb#snippet1)]
49+
***
2450

2551
--------------------------------------------------------------------------------
2652

2753
[!include[Structure](../includes/spreadsheet/structure.md)]
2854

2955
--------------------------------------------------------------------------------
30-
## Sample Code
56+
## Sample Code
3157

3258
Following is the complete sample code in both C\# and Visual Basic.
3359

3460
### [C#](#tab/cs)
35-
[!code-csharp[](../../samples/spreadsheet/insert_a_new_worksheet/cs/Program.cs)]
61+
[!code-csharp[](../../samples/spreadsheet/insert_a_new_worksheet/cs/Program.cs#snippet0)]
3662

3763
### [Visual Basic](#tab/vb)
38-
[!code-vb[](../../samples/spreadsheet/insert_a_new_worksheet/vb/Program.vb)]
64+
[!code-vb[](../../samples/spreadsheet/insert_a_new_worksheet/vb/Program.vb#snippet0)]
65+
***
3966

4067
--------------------------------------------------------------------------------
41-
## See also
68+
## See also
4269

4370

4471
[Open XML SDK class library reference](/office/open-xml/open-xml-sdk)

samples/spreadsheet/insert_a_new_worksheet/cs/Program.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
using DocumentFormat.OpenXml.Spreadsheet;
33
using System.Linq;
44

5+
InsertWorksheet(args[0]);
6+
57
// Given a document name, inserts a new worksheet.
8+
// <Snippet0>
69
static void InsertWorksheet(string docName)
710
{
11+
// <Snippet1>
812
// Open the document for editing.
913
using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))
14+
// </Snippet1>
1015
{
1116
WorkbookPart workbookPart = spreadSheet.WorkbookPart ?? spreadSheet.AddWorkbookPart();
1217
// Add a blank WorksheetPart.
@@ -31,5 +36,4 @@ static void InsertWorksheet(string docName)
3136
sheets.Append(sheet);
3237
}
3338
}
34-
35-
InsertWorksheet(args[0]);
39+
// </Snippet0>
Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
Imports DocumentFormat.OpenXml.Packaging
22
Imports DocumentFormat.OpenXml.Spreadsheet
3+
Imports System.Linq
34

45
Module Program
56
Sub Main(args As String())
7+
InsertWorksheet(args(0))
68
End Sub
79

8-
9-
1010
' Given a document name, inserts a new worksheet.
11-
Public Sub InsertWorksheet(ByVal docName As String)
11+
' <Snippet0>
12+
Sub InsertWorksheet(docName As String)
13+
' <Snippet1>
1214
' Open the document for editing.
13-
Dim spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)
14-
15-
Using (spreadSheet)
15+
Using spreadSheet As SpreadsheetDocument = SpreadsheetDocument.Open(docName, True)
16+
' </Snippet1>
17+
Dim workbookPart As WorkbookPart = If(spreadSheet.WorkbookPart, spreadSheet.AddWorkbookPart())
1618
' Add a blank WorksheetPart.
17-
Dim newWorksheetPart As WorksheetPart = spreadSheet.WorkbookPart.AddNewPart(Of WorksheetPart)()
19+
Dim newWorksheetPart As WorksheetPart = workbookPart.AddNewPart(Of WorksheetPart)()
1820
newWorksheetPart.Worksheet = New Worksheet(New SheetData())
19-
' newWorksheetPart.Worksheet.Save()
2021

21-
Dim sheets As Sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild(Of Sheets)()
22-
Dim relationshipId As String = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart)
22+
Dim sheets As Sheets = If(workbookPart.Workbook.GetFirstChild(Of Sheets)(), workbookPart.Workbook.AppendChild(New Sheets()))
23+
Dim relationshipId As String = workbookPart.GetIdOfPart(newWorksheetPart)
2324

2425
' Get a unique ID for the new worksheet.
2526
Dim sheetId As UInteger = 1
26-
If (sheets.Elements(Of Sheet).Count > 0) Then
27-
sheetId = sheets.Elements(Of Sheet).Select(Function(s) s.SheetId.Value).Max + 1
27+
If sheets.Elements(Of Sheet)().Count() > 0 Then
28+
sheetId = sheets.Elements(Of Sheet)().Select(Function(s) s.SheetId?.Value).Max() + 1
2829
End If
2930

3031
' Give the new worksheet a name.
31-
Dim sheetName As String = ("Sheet" + sheetId.ToString())
32+
Dim sheetName As String = "Sheet" & sheetId
3233

3334
' Append the new worksheet and associate it with the workbook.
34-
Dim sheet As Sheet = New Sheet
35-
sheet.Id = relationshipId
36-
sheet.SheetId = sheetId
37-
sheet.Name = sheetName
35+
Dim sheet As New Sheet() With {
36+
.Id = relationshipId,
37+
.SheetId = sheetId,
38+
.Name = sheetName
39+
}
3840
sheets.Append(sheet)
3941
End Using
4042
End Sub
41-
End Module
43+
' </Snippet0>
44+
End Module
45+
46+
47+

0 commit comments

Comments
 (0)