|
| 1 | +--- |
| 2 | +id: deleting-worksheets-from-spreadsheet |
| 3 | +url: editor/java/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 Java version 26.1 - 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 Java |
| 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 26.1](https://releases.groupdocs.com/editor/java/release-notes/2026/groupdocs-editor-for-java-26-1-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/java/load-document/) to the constructor of the [`Editor`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editor/) class, then worksheet is specified by its number in the [`SpreadsheetEditOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheeteditoptions/), and then document is converted to the editable form, represented by the [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.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/java/com.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/java/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 26.1](https://releases.groupdocs.com/editor/java/release-notes/2026/groupdocs-editor-for-java-26-1-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/java/com.groupdocs.editor.options/spreadsheetsaveoptions/) class — [`WorksheetNumbersToDelete`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumbersToDelete-int---) 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/java/com.groupdocs.editor/editor/#save-java.io.OutputStream-) method. |
| 24 | + |
| 25 | +Need to mention that the worksheet numbers in a [`WorksheetNumbersToDelete`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumbersToDelete-int---) property are 1-based. For instance, for removing the first and fourth worksheets the [`WorksheetNumbersToDelete`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumbersToDelete-int---) 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/java/com.groupdocs.editor/editabledocument/) class [using one of its static methods](https://docs.groupdocs.com/editor/java/create-editabledocument-from-file-or-markup/), creates and adjusts the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/), and passes the [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editabledocument/) instance, [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/) instance, and output stream or file path for writing to the [`Editor.save()`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editor/#save-java.io.OutputStream-) method. |
| 30 | +2. If the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumber-int-) property in the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/) instance is set to the default value `0`, the GroupDocs.Editor generates a new spreadsheet with one worksheet. The [`WorksheetNumbersToDelete`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumbersToDelete-int---) property is ignored regardless of its value. |
| 31 | +3. If the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumber-int-) property in the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/java/com.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/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setInsertAsNewWorksheet-boolean-) property, the GroupDocs.Editor replaces old worksheet, specified by the value of the [`WorksheetNumber`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumber-int-) property, on the new one, taken from the [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.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 worksheet insertions and rearrangements are finished, the GroupDocs.Editor reads the value of the [`WorksheetNumbersToDelete`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor.options/spreadsheetsaveoptions/#setWorksheetNumbersToDelete-int---) 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/java/com.groupdocs.editor/editabledocument/), then HTML-markup is emitted from the [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editabledocument/) instance, modified, and then edited HTML-markup is converted back to the another [`EditableDocument`](https://reference.groupdocs.com/editor/java/com.groupdocs.editor/editabledocument/) instance. From this point two instances of the [`SpreadsheetSaveOptions`](https://reference.groupdocs.com/editor/java/com.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/java/com.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 | +{{< tabs "code-example">}} |
| 39 | +{{< tab "DeleteWorksheetsFromLoadedAndEditedSpreadsheet.java" >}} |
| 40 | +```java |
| 41 | +import com.groupdocs.editor.EditableDocument; |
| 42 | +import com.groupdocs.editor.Editor; |
| 43 | +import com.groupdocs.editor.examples.Constants; |
| 44 | +import com.groupdocs.editor.formats.SpreadsheetFormats; |
| 45 | +import com.groupdocs.editor.options.SpreadsheetEditOptions; |
| 46 | +import com.groupdocs.editor.options.SpreadsheetLoadOptions; |
| 47 | +import com.groupdocs.editor.options.SpreadsheetSaveOptions; |
| 48 | + |
| 49 | +public class DeleteWorksheetsFromLoadedAndEditedSpreadsheet { |
| 50 | + public static void edit() { |
| 51 | + |
| 52 | + SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions(); |
| 53 | + |
| 54 | + Editor editor = new Editor("Input3Worksheets.xlsx", loadOptions); |
| 55 | + // Prepare edit options: open 2nd worksheet (WorksheetIndex is 0-based) |
| 56 | + SpreadsheetEditOptions editOptions = new SpreadsheetEditOptions(); |
| 57 | + editOptions.setWorksheetIndex(1); |
| 58 | + |
| 59 | + try { |
| 60 | + |
| 61 | + EditableDocument worksheet2OpenedForEdit = editor.edit(editOptions); |
| 62 | + // Get embedded HTML of the worksheet |
| 63 | + String originalHtml = worksheet2OpenedForEdit.getEmbeddedHtml(); |
| 64 | + |
| 65 | + // Emulate HTML edit |
| 66 | + String editedHtml = originalHtml.replace("2nd row", "Edited 2nd row at 1st column"); |
| 67 | + |
| 68 | + // Build EditableDocument from edited markup |
| 69 | + try { |
| 70 | + EditableDocument worksheet2AfterEdit = EditableDocument.fromMarkup(editedHtml, null); |
| 71 | + // ---- Save #1: insert edited worksheet as new, without deleting anything |
| 72 | + SpreadsheetSaveOptions saveOptionsWithoutDelete = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx); |
| 73 | + saveOptionsWithoutDelete.setWorksheetNumber(2); // 1-based |
| 74 | + saveOptionsWithoutDelete.setInsertAsNewWorksheet(true); // don't overwrite, insert new |
| 75 | + String outputWorksheets_without_delete = Constants.getOutputDirectoryPath("Output4Worksheets-without-delete.xlsx"); |
| 76 | + editor.save( |
| 77 | + worksheet2AfterEdit, |
| 78 | + outputWorksheets_without_delete, |
| 79 | + saveOptionsWithoutDelete |
| 80 | + ); |
| 81 | + |
| 82 | + // ---- Save #2: insert edited worksheet as new, and delete 1st + last worksheet |
| 83 | + SpreadsheetSaveOptions saveOptionsWithDelete = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx); |
| 84 | + saveOptionsWithDelete.setWorksheetNumber(2); // 1-based |
| 85 | + saveOptionsWithDelete.setInsertAsNewWorksheet(true); |
| 86 | + |
| 87 | + // After insertion, total worksheets become 4 => delete #1 and #4 |
| 88 | + saveOptionsWithDelete.setWorksheetNumbersToDelete(new int[]{1, 4}); |
| 89 | + String outputWorksheets_with_delete = Constants.getOutputDirectoryPath("Output2Worksheets-with-delete.xlsx"); |
| 90 | + editor.save( |
| 91 | + worksheet2AfterEdit, |
| 92 | + outputWorksheets_with_delete, |
| 93 | + saveOptionsWithDelete |
| 94 | + ); |
| 95 | + }finally { |
| 96 | + worksheet2OpenedForEdit.dispose(); |
| 97 | + } |
| 98 | + }finally { |
| 99 | + editor.dispose(); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public static void main(String[] args){ |
| 104 | + edit(); |
| 105 | + } |
| 106 | +} |
| 107 | +``` |
| 108 | +{{< /tab >}} |
| 109 | +{{< tab "Input3Worksheets.xlsxz" >}} |
| 110 | +{{< tab-text >}} |
| 111 | +`Input3Worksheets.xlsx` is sample file used in this example. Click [here](/editor/java/sample-files/Input3Worksheets.xlsx) to download it. |
| 112 | +{{< /tab-text >}} |
| 113 | +{{< /tab >}} |
| 114 | +{{< tab "Output4Worksheets-without-delete.xlsx" >}} |
| 115 | +{{< tab-text >}} |
| 116 | +`Output4Worksheets-without-delete.xlsx` is edited document without deleted worksheets. Click [here](/editor/java/sample-files/Output4Worksheets-without-delete.xlsx) to download it. |
| 117 | +{{< /tab-text >}} |
| 118 | +{{< /tab >}} |
| 119 | +{{< tab "Output2Worksheets-with-delete.xlsx" >}} |
| 120 | +{{< tab-text >}} |
| 121 | +`Output2Worksheets-with-delete.xlsx` is edited document with deleted worksheets. Click [here](/editor/java/sample-files/Output2Worksheets-with-delete.xlsx) to download it. |
| 122 | +{{< /tab-text >}} |
| 123 | +{{< /tab >}} |
| 124 | +{{< /tabs >}} |
0 commit comments