Skip to content

Commit 57a421b

Browse files
EDITORNET-2970 - Write article about deleting worksheets for public docs
1 parent 728326f commit 57a421b

5 files changed

Lines changed: 157 additions & 1 deletion

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
id: deleting-worksheets-from-spreadsheet
3+
url: editor/net/deleting-worksheets-from-spreadsheet
4+
title: Deleting worksheets from spreadsheet
5+
weight: 3
6+
description: "This article describes the new feature of the GroupDocs.Editor for .NET version 25.11 - deleting (removing) one or many worksheets from the loaded and edited spreadsheet (workbook) during its saving to the output format"
7+
keywords: Edit Excel, delete tab from Excel workbook, remove tab from Excel workbook, delete worksheet from spreadsheet, remove worksheet from spreadsheet
8+
productName: GroupDocs.Editor for .NET
9+
hideChildren: False
10+
toc: True
11+
---
12+
A **spreadsheet**, also known as a **workbook** — is a [family of the document formats](https://docs.fileformat.com/spreadsheet/), designed to work with tabular data. [XLS](https://docs.fileformat.com/spreadsheet/xls/), [XLSX](https://docs.fileformat.com/spreadsheet/xlsx/), [ODS](https://docs.fileformat.com/spreadsheet/ods/), [CSV](https://docs.fileformat.com/spreadsheet/csv/) formats are the most common examples of such document formats, while Microsoft Excel, LibreOffice Calc, Apache OpenOffice Calc are examples of table processors — programs, which allow the creation and editing of such documents. GroupDocs.Editor has an ability to edit existing spreadsheet documents from its beginning, and starting from the [version 24.6](https://releases.groupdocs.com/editor/net/release-notes/2024/groupdocs-editor-for-net-24-6-release-notes/) it has obtained an ability not only to edit already existing spreadsheets, but also to create new spreadsheets from scratch. And starting from the [version 25.11](https://releases.groupdocs.com/editor/net/release-notes/2025/groupdocs-editor-for-net-25-11-release-notes/) the GroupDocs.Editor has obtained a feature to delete worksheets from the edited spreadsheet during saving.
13+
14+
Need to keep in mind that not every spreadsheet may have the worksheets. For example, text-based separator-delimited formats like [CSV](https://docs.fileformat.com/spreadsheet/csv/) and [TSV](https://docs.fileformat.com/spreadsheet/tsv/) are basically the text files, they have no worksheets, so the worksheet cannot be removed from them. But almost all of the binary spreadsheet formats like XLS, XLSX and ODS do have them.
15+
16+
In GroupDocs.Editor the user edits one worksheet at a time — the spreadsheet [is loaded](https://docs.groupdocs.com/editor/net/load-document/) to the constructor of the [`Editor`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editor) class, then worksheet is specified by its number in the [`SpreadsheetEditOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheeteditoptions), and then document is converted to the editable form, represented by the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) class. Straight and simple. However, when the content of the worksheet was edited by the user and passed back to the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument class, there are two options:
17+
18+
1. Users can save the edited content of the worksheet as a new spreadsheet with this single worksheet inside. This is the default behaviour that was present in the GroupDocs.Editor from its beginning.
19+
2. Users can save the edited worksheet by inserting it to the original spreadsheet. This was made possible starting from the [version 20.10](https://releases.groupdocs.com/editor/net/release-notes/2020/groupdocs-editor-for-net-20-10-release-notes/) and is described in a [separate article](https://docs.groupdocs.com/editor/net/inserting-edited-worksheet-into-existing-spreadsheet/). This second option also has two sub-options:
20+
1. The edited worksheet can *replace* the original worksheet in the input spreadsheet. For instance, in the loaded spreadsheet with 3 worksheets the user has chosen the 2nd one for edit; this worksheet was edited and then inserted back to the input spreadsheet, replacing the original 2nd worksheet onto the edited one.
21+
2. The edited worksheet can be inserted into the input spreadsheet to stay *together* with the original one. For instance, in the loaded spreadsheet with 3 worksheets the user has chosen the 2nd one for edit; this worksheet was edited and then inserted back to the input spreadsheet, so now the spreadsheet contains 4 worksheets: the first, fourth, and two versions of the second (original and edited).
22+
23+
Starting from the [version 25.11](https://releases.groupdocs.com/editor/net/release-notes/2025/groupdocs-editor-for-net-25-11-release-notes/), when the 2nd option (inserting edited worksheet into the original spreadsheet), users also can delete particular worksheet(s) from it. A new property was added to the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) class — `WorksheetNumbersToDelete` of `int[]` type. By default it has a `null` value, which means that no worksheets should be removed. However, when it has one or more valid worksheet numbers, the worksheets with these numbers are removed while calling the [`Editor.Save()`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editor/save) method.
24+
25+
Need to mention that the worksheet numbers in a `WorksheetNumbersToDelete` property are 1-based. For instance, for removing the first and fourth worksheets the `WorksheetNumbersToDelete` property should have a `new int[2] {1, 4}` value.
26+
27+
With the new worksheet removal feature the GroupDocs.Editor performs the next algorithm during saving the spreadsheet:
28+
29+
1. User edits a content of the worksheet in the WYSIWYG-editor, passes the edited content to the instance of the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) class [using one of its static methods](https://docs.groupdocs.com/editor/net/create-editabledocument-from-file-or-markup/), creates and adjusts the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions), and passes the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) instance, [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) instance, and output stream or file path for writing to the [`Editor.Save()`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editor/save) method.
30+
2. If the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions/worksheetnumber) property in the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) instance is set to the default value `0`, the GroupDocs.Editor generates a new spreadsheet with one worksheet. The `WorksheetNumbersToDelete` property is ignored regardless of its value.
31+
3. If the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions/worksheetnumber) property in the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) instance has some non-zero value, the GroupDocs.Editor treats it as a command to insert the edited worksheet into the original spreadsheet.
32+
4. Depending on the [`InsertAsNewWorksheet`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions/insertasnewworksheet/) property, the GroupDocs.Editor replaces old worksheet, specified by the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions/worksheetnumber) property, on the new one, taken from the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) instance, or puts the new worksheet to be placed among existing worksheets without replacing any of them.
33+
5. Finally, when all worksheets insertions and rearrangements are finished, the GroupDocs.Editor reads the value of the `WorksheetNumbersToDelete` property, iterates over all numbers and removes specified worksheets from the spreadsheet.
34+
6. The final spreadsheet, with updated and deleted worksheets, is written to the output stream or file.
35+
36+
Example below shows full roundtrip of input XLSX file: spreadsheet with 3 worksheets inside is loaded, its second worksheet is converted to the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument), then HTML-markup is emitted from the [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) instance, edited, and then edited HTML-markup is converted back to the another [`EditableDocument`](https://reference.groupdocs.com/editor/net/groupdocs.editor/editabledocument) instance. From this point two instances of the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) class are created — one is intended for generating a first version of an output spreadsheet with a new worksheet and without deletion and finally it has 4 worksheets in total (3 original + 1 edited). The second [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/net/groupdocs.editor.options/spreadsheetsaveoptions) instance has the WorksheetNumbersToDelete property set to remove the first and last worksheets, so the 2nd version of an output spreadsheet will have 2 worksheets in total (1 original + 1 edited).
37+
38+
Input XLSX as well as two versions of the output XLSX files may be downloaded here:
39+
40+
- [Input3Worksheets.xlsx](/editor/net/sample-files/Input3Worksheets.xlsx)
41+
- [Output4Worksheets-without-delete.xlsx](/editor/net/sample-files/Output4Worksheets-without-delete.xlsx)
42+
- [Output2Worksheets-with-delete.xlsx](/editor/net/sample-files/Output2Worksheets-with-delete.xlsx)
43+
44+
{{< tabs "DeleteWorksheetsFromLoadedAndEditedSpreadsheet">}}
45+
{{< tab "C#" >}}
46+
```csharp
47+
using GroupDocs.Editor;
48+
using GroupDocs.Editor.Formats;
49+
using GroupDocs.Editor.Options;
50+
// ...
51+
52+
// Load input spreadsheet to the Editor and specify loading options
53+
using (Editor editor = new Editor("Input3Worksheets.xlsx", new SpreadsheetLoadOptions()))
54+
{
55+
// Prepare edit options and set 2nd worksheet to edit
56+
SpreadsheetEditOptions editOptions = new SpreadsheetEditOptions();
57+
editOptions.WorksheetIndex = 1;//2nd worksheet to edit, here WorksheetIndex is 0-based due to historical reasons
58+
59+
//generate EditableDocument with original content of 2nd worksheet
60+
using (EditableDocument worksheet2OpenedForEdit = editor.Edit(editOptions))
61+
{
62+
// Get the HTML-markup from the EditableDocument with original content
63+
string originalHtmlContentOf2ndWorksheet = worksheet2OpenedForEdit.GetEmbeddedHtml();
64+
65+
//emulate HTML content editing in WYSIWYG-editor in browser or somewhere else
66+
string editedHtmlContentOf2ndWorksheet = originalHtmlContentOf2ndWorksheet.Replace("2nd row", "Edited 2nd row at 1st column");
67+
68+
//generate EditableDocument with edited content of 2nd worksheet
69+
using (EditableDocument worksheet2AfterEdit = EditableDocument.FromMarkup(editedHtmlContentOf2ndWorksheet))
70+
{
71+
//prepare save options without deletions
72+
SpreadsheetSaveOptions saveOptionsWithoutDelete = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);
73+
// let it be the 2nd worksheet...
74+
saveOptionsWithoutDelete.WorksheetNumber = 2;//here WorksheetNumber is 1-based
75+
//... and we also save the original 2nd worksheet, which is pushed to the 3rd position
76+
saveOptionsWithoutDelete.InsertAsNewWorksheet = true;
77+
78+
// So now the spreadsheet must have 4 worksheets, not 3. Save it to file
79+
editor.Save(worksheet2AfterEdit, "Output4Worksheets-without-delete.xlsx", saveOptionsWithoutDelete);
80+
81+
// Create another save options, with deletions at this time
82+
SpreadsheetSaveOptions saveOptionsWithDelete = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);
83+
// let the new worksheet will be 2nd for now, but after deleting it will became the 1st later
84+
saveOptionsWithDelete.WorksheetNumber = 2;
85+
86+
//... and we also save the original 2nd worksheet, which will be next sibling to the newly inserted
87+
saveOptionsWithDelete.InsertAsNewWorksheet = true;
88+
89+
// delete the 1st and last worksheet
90+
saveOptionsWithDelete.WorksheetNumbersToDelete = new int[2]
91+
{
92+
1,//1st worksheet
93+
4//last worksheet is 4, not 3, because we inserted the edited 2nd worksheet as the new worksheet instance, without rewriting the original 2nd worksheet
94+
};
95+
// Save it again to distinct file. Output XLSX should have 2 worksheets now.
96+
editor.Save(worksheet2AfterEdit, "Output2Worksheets-with-delete.xlsx", saveOptionsWithDelete);
97+
}
98+
}
99+
}
100+
```
101+
{{< /tab >}}
102+
{{< tab "VB.NET">}}
103+
```vb
104+
Imports GroupDocs.Editor
105+
Imports GroupDocs.Editor.Formats
106+
Imports GroupDocs.Editor.Options
107+
' ...
108+
109+
' Load input spreadsheet to the Editor and specify loading options
110+
Using editor As New Editor("Input3Worksheets.xlsx", New SpreadsheetLoadOptions())
111+
' Prepare edit options and set 2nd worksheet to edit
112+
Dim editOptions As New SpreadsheetEditOptions()
113+
editOptions.WorksheetIndex = 1 '2nd worksheet to edit, here WorksheetIndex is 0-based due to historical reasons
114+
115+
' generate EditableDocument with original content of 2nd worksheet
116+
Using worksheet2OpenedForEdit As EditableDocument = editor.Edit(editOptions)
117+
' Get the HTML-markup from the EditableDocument with original content
118+
Dim originalHtmlContentOf2ndWorksheet As String = worksheet2OpenedForEdit.GetEmbeddedHtml()
119+
120+
' emulate HTML content editing in WYSIWYG-editor in browser or somewhere else
121+
Dim editedHtmlContentOf2ndWorksheet As String = originalHtmlContentOf2ndWorksheet.Replace("2nd row", "Edited 2nd row at 1st column")
122+
123+
' generate EditableDocument with edited content of 2nd worksheet
124+
Using worksheet2AfterEdit As EditableDocument = EditableDocument.FromMarkup(editedHtmlContentOf2ndWorksheet)
125+
' prepare save options without deletions
126+
Dim saveOptionsWithoutDelete As New SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx)
127+
128+
' let it be the 2nd worksheet...
129+
saveOptionsWithoutDelete.WorksheetNumber = 2 'here WorksheetNumber is 1-based
130+
'... and we also save the original 2nd worksheet, which is pushed to the 3rd position
131+
saveOptionsWithoutDelete.InsertAsNewWorksheet = True
132+
133+
' So now the spreadsheet must have 4 worksheets, not 3. Save it to file
134+
editor.Save(worksheet2AfterEdit, "Output4Worksheets-without-delete.xlsx", saveOptionsWithoutDelete)
135+
136+
' Create another save options, with deletions at this time
137+
Dim saveOptionsWithDelete As New SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx)
138+
' let the new worksheet will be 2nd for now, but after deleting it will became the 1st later
139+
saveOptionsWithDelete.WorksheetNumber = 2
140+
'... and we also save the original 2nd worksheet, which will be next sibling to the newly inserted
141+
saveOptionsWithDelete.InsertAsNewWorksheet = True
142+
143+
' delete the 1st and last worksheet
144+
saveOptionsWithDelete.WorksheetNumbersToDelete = New Int32(1) {
145+
1, '1st worksheet
146+
4 'last worksheet is 4, not 3, because we inserted the edited 2nd worksheet as the new worksheet instance, without rewriting the original 2nd worksheet
147+
}
148+
' Save it again to distinct file. Output XLSX should have 2 worksheets now.
149+
editor.Save(worksheet2AfterEdit, "Output2Worksheets-with-delete.xlsx", saveOptionsWithDelete)
150+
End Using
151+
End Using
152+
End Using
153+
```
154+
{{< /tab >}}
155+
{{< /tabs >}}
156+

net/developer-guide/edit-document/edit-excel/inserting-edited-worksheet-into-existing-spreadsheet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ url: editor/net/inserting-edited-worksheet-into-existing-spreadsheet
44
title: Inserting edited worksheet into existing spreadsheet
55
weight: 1
66
description: "This article describes the new feature of the GroupDocs.Editor for .NET version 20.10 - inserting an edited worksheet into existing spreadsheet"
7-
keywords: Edit Excel, insert tab into Escel, insert worksheet into Excel, insert worksheet into spreadsheet
7+
keywords: Edit Excel, insert tab into Excel, insert worksheet into Excel, insert worksheet into workbook, insert worksheet into spreadsheet
88
productName: GroupDocs.Editor for .NET
99
hideChildren: False
1010
toc: True
14 KB
Binary file not shown.
12.5 KB
Binary file not shown.
15.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)