I am finding this the best way to update a template file using Apache POI. By template file I mean a nicely formatted .xlsx file you want to update by inserting values.
Steps are: Copy the template file; open as a file; update content; write to null outstream; close file.
Note the step where it writes to a null output stream - this will push the updates back to the file - without this step the copied template file won't be updated. The template file won't be updated if the constructor uses an input stream rather than a file.
//Copy the template to the final file path
File tempFile = new File("UpdatedTemplate.xlsx");
Files.copy(template.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
XSSFWorkbook wb;
wb = new XSSFWorkbook(tempFile); <-- Will update if this uses a file but not if uses a input stream
//update the content
wb.write(OutputStream.nullOutputStream());
wb.close();
Hope this is of use - I was previously using an input stream to prevent the writing back to the template file, rather than copying the template prior to the update, and find this much quicker as it avoids the input file constructor.
Anyone know a better method?