Related blog post: Edit Excel Spreadsheets in Java
Last active
April 4, 2022 03:50
-
-
Save GroupDocsGists/9fcd27dc5fb28ccbf8a44bbe9d988ece to your computer and use it in GitHub Desktop.
How to Edit Excel Spreadsheets in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Edit the Excel XLS/XLSX documents in Java | |
SpreadsheetLoadOptions loadOptions = new SpreadsheetLoadOptions(); | |
loadOptions.setPassword("password-if-any"); | |
// Loading Spreadsheet | |
Editor editor = new Editor("path/sample_sheet.xlsx", loadOptions); | |
// Edit 1st tab of the Spreadsheet | |
SpreadsheetEditOptions editOptions = new SpreadsheetEditOptions(); | |
editOptions.setWorksheetIndex(0); // index is 0-based, so this is 1st tab | |
EditableDocument firstTab = editor.edit(editOptions); | |
String bodyContent = firstTab.getBodyContent(); | |
String allContent = firstTab.getContent(); | |
List<IImageResource> onlyImages = firstTab.getImages(); | |
List<IHtmlResource> allResourcesTogether = firstTab.getAllResources(); | |
String editedSheetContent = allContent.replace("Old Company Name","New Company Name"); | |
EditableDocument editedDoc = EditableDocument.fromMarkup(editedSheetContent, null); | |
SpreadsheetSaveOptions saveOptions = new SpreadsheetSaveOptions(SpreadsheetFormats.Xlsx); | |
saveOptions.setPassword("new-password"); | |
editor.save(editedDoc, "path/edited_spreadsheet.xlsx", saveOptions); | |
firstTab.dispose(); | |
editor.dispose(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment