Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active April 4, 2022 03:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/9fcd27dc5fb28ccbf8a44bbe9d988ece to your computer and use it in GitHub Desktop.
Save GroupDocsGists/9fcd27dc5fb28ccbf8a44bbe9d988ece to your computer and use it in GitHub Desktop.
How to Edit Excel Spreadsheets in Java
// 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