A spreadsheet, also known as a workbook — is a family of the document formats, designed to work with tabular data. XLS, XLSX, ODS, 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 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 the GroupDocs.Editor has obtained a feature to delete worksheets from the edited spreadsheet during saving.
Need to keep in mind that not every spreadsheet may have the worksheets. For example, text-based separator-delimited formats like CSV and 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.
In GroupDocs.Editor the user edits one worksheet at a time — the spreadsheet is loaded to the constructor of the Editor class, then worksheet is specified by its number in the SpreadsheetEditOptions, and then document is converted to the editable form, represented by the EditableDocument class. Straight and simple. However, when the content of the worksheet was edited by the user and passed back to the EditableDocument class, there are two options:
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.
Users can save the edited worksheet by inserting it to the original spreadsheet. This was made possible starting from the version 20.10 and is described in a separate article. This second option also has two sub-options:
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.
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).
Starting from the version 25.11, 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 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() method.
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.
With the new worksheet removal feature the GroupDocs.Editor performs the next algorithm during saving the spreadsheet:
If the value of the WorksheetNumber property in the 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.
If the value of the WorksheetNumber property in the SpreadsheetSaveOptions instance has some non-zero value, the GroupDocs.Editor treats it as a command to insert the edited worksheet into the original spreadsheet.
Depending on the InsertAsNewWorksheet property, the GroupDocs.Editor replaces old worksheet, specified by the value of the WorksheetNumber property, on the new one, taken from the EditableDocument instance, or puts the new worksheet to be placed among existing worksheets without replacing any of them.
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.
The final spreadsheet, with updated and deleted worksheets, is written to the output stream or file.
Example below shows full roundtrip of input XLSX file: spreadsheet with 3 worksheets inside is loaded, its second worksheet is converted to the EditableDocument, then HTML-markup is emitted from the EditableDocument instance, edited, and then edited HTML-markup is converted back to the another EditableDocument instance. From this point two instances of the 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 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).
Input XLSX as well as two versions of the output XLSX files may be downloaded here:
usingGroupDocs.Editor;usingGroupDocs.Editor.Formats;usingGroupDocs.Editor.Options;// ...// Load input spreadsheet to the Editor and specify loading optionsusing(Editoreditor=newEditor("Input3Worksheets.xlsx",newSpreadsheetLoadOptions())){// Prepare edit options and set 2nd worksheet to editSpreadsheetEditOptionseditOptions=newSpreadsheetEditOptions();editOptions.WorksheetIndex=1;//2nd worksheet to edit, here WorksheetIndex is 0-based due to historical reasons//generate EditableDocument with original content of 2nd worksheetusing(EditableDocumentworksheet2OpenedForEdit=editor.Edit(editOptions)){// Get the HTML-markup from the EditableDocument with original contentstringoriginalHtmlContentOf2ndWorksheet=worksheet2OpenedForEdit.GetEmbeddedHtml();//emulate HTML content editing in WYSIWYG-editor in browser or somewhere elsestringeditedHtmlContentOf2ndWorksheet=originalHtmlContentOf2ndWorksheet.Replace("2nd row","Edited 2nd row at 1st column");//generate EditableDocument with edited content of 2nd worksheetusing(EditableDocumentworksheet2AfterEdit=EditableDocument.FromMarkup(editedHtmlContentOf2ndWorksheet)){//prepare save options without deletionsSpreadsheetSaveOptionssaveOptionsWithoutDelete=newSpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);// let it be the 2nd worksheet...saveOptionsWithoutDelete.WorksheetNumber=2;//here WorksheetNumber is 1-based//... and we also save the original 2nd worksheet, which is pushed to the 3rd positionsaveOptionsWithoutDelete.InsertAsNewWorksheet=true;// So now the spreadsheet must have 4 worksheets, not 3. Save it to fileeditor.Save(worksheet2AfterEdit,"Output4Worksheets-without-delete.xlsx",saveOptionsWithoutDelete);// Create another save options, with deletions at this timeSpreadsheetSaveOptionssaveOptionsWithDelete=newSpreadsheetSaveOptions(SpreadsheetFormats.Xlsx);// let the new worksheet will be 2nd for now, but after deleting it will became the 1st latersaveOptionsWithDelete.WorksheetNumber=2;//... and we also save the original 2nd worksheet, which will be next sibling to the newly insertedsaveOptionsWithDelete.InsertAsNewWorksheet=true;// delete the 1st and last worksheetsaveOptionsWithDelete.WorksheetNumbersToDelete=newint[2]{1,//1st worksheet4//last worksheet is 4, not 3, because we inserted the edited 2nd worksheet as the new worksheet instance, without rewriting the original 2nd worksheet};// Save it again to distinct file. Output XLSX should have 2 worksheets now.editor.Save(worksheet2AfterEdit,"Output2Worksheets-with-delete.xlsx",saveOptionsWithDelete);}}}
ImportsGroupDocs.EditorImportsGroupDocs.Editor.FormatsImportsGroupDocs.Editor.Options' ...
' Load input spreadsheet to the Editor and specify loading options
UsingeditorAsNewEditor("Input3Worksheets.xlsx",NewSpreadsheetLoadOptions())' Prepare edit options and set 2nd worksheet to edit
DimeditOptionsAsNewSpreadsheetEditOptions()editOptions.WorksheetIndex=1'2nd worksheet to edit, here WorksheetIndex is 0-based due to historical reasons
' generate EditableDocument with original content of 2nd worksheet
Usingworksheet2OpenedForEditAsEditableDocument=editor.Edit(editOptions)' Get the HTML-markup from the EditableDocument with original content
DimoriginalHtmlContentOf2ndWorksheetAsString=worksheet2OpenedForEdit.GetEmbeddedHtml()' emulate HTML content editing in WYSIWYG-editor in browser or somewhere else
DimeditedHtmlContentOf2ndWorksheetAsString=originalHtmlContentOf2ndWorksheet.Replace("2nd row","Edited 2nd row at 1st column")' generate EditableDocument with edited content of 2nd worksheet
Usingworksheet2AfterEditAsEditableDocument=EditableDocument.FromMarkup(editedHtmlContentOf2ndWorksheet)' prepare save options without deletions
DimsaveOptionsWithoutDeleteAsNewSpreadsheetSaveOptions(SpreadsheetFormats.Xlsx)' let it be the 2nd worksheet...
saveOptionsWithoutDelete.WorksheetNumber=2'here WorksheetNumber is 1-based
'... and we also save the original 2nd worksheet, which is pushed to the 3rd position
saveOptionsWithoutDelete.InsertAsNewWorksheet=True' So now the spreadsheet must have 4 worksheets, not 3. Save it to file
editor.Save(worksheet2AfterEdit,"Output4Worksheets-without-delete.xlsx",saveOptionsWithoutDelete)' Create another save options, with deletions at this time
DimsaveOptionsWithDeleteAsNewSpreadsheetSaveOptions(SpreadsheetFormats.Xlsx)' let the new worksheet will be 2nd for now, but after deleting it will became the 1st later
saveOptionsWithDelete.WorksheetNumber=2'... and we also save the original 2nd worksheet, which will be next sibling to the newly inserted
saveOptionsWithDelete.InsertAsNewWorksheet=True' delete the 1st and last worksheet
saveOptionsWithDelete.WorksheetNumbersToDelete=NewInt32(1){1,'1st worksheet
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
}' Save it again to distinct file. Output XLSX should have 2 worksheets now.
editor.Save(worksheet2AfterEdit,"Output2Worksheets-with-delete.xlsx",saveOptionsWithDelete)EndUsingEndUsingEndUsing
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.