I have a template.xls
file that I'm adding data to from some database queries. I add the data and generate a new file named yyyyMMddHHmmss.xls
. This works great. The file size is getting large so I'm trying to do the same with an xlsx
file. When I generate the file the first time it works great. If I run the process again (even if I restart my java app) it's somehow retaining the last file in memory and appending the data to that file. In both cases it's pulling the source file from template.xls(x)
which is an unmodified file.
The code between the two is identical except I'm passing in xlsx
instead of xls
in the latter case.
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(Objects.requireNonNull(classLoader.getResource("template.xlsx")).getFile());
Workbook workbook = WorkbookFactory.create(file);
// write data
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
String currentDate = formatter.format(date);
FileOutputStream fileOutputStream = new FileOutputStream(currentDate + ".xlsx");
workbook.write(fileOutputStream);
fileOutputStream.close();
workbook.close();
I'm using Java 8u201
and org.apache.poi:poi:4.1.0
(also tried 4.0.1
)