I am writing a code for populating a workbook then writing it out to an excel file. However, when I run the code for the second time, it throws the error saying "sheet already exists".
Here is the basic code -
File file = getFileObjectForExistingXLSXTemplate(existingTemplateLocation);
Workbook workbook = null;
workbook = WorkbookFactory.create(file);
populateWorkbookThenWriteOutToExcel(workbook, someOtherDestinationDifferentFromTemplateLocation);
workbook.close();
file.delete();
Please help me in understanding how to clear the workbook from memory when I run the code again after 1st time.
Initially, the existing template only has "Sheet 1". The code writes "Sheet 2" to it. First time, everything runs great. But second time when I run the same code immediately, it says "Sheet 2 already exists".