Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POI read file without updating file timestamp

It appears that one can open an Excel file using

Workbook wb = WorkbookFactory.create(myFile);

...but then, even if no changes are made, a call to

wb.close();

...to release resources will result in the file on the file system being updated (at least the timestamp will be, if nothing else). The only way I have found around this is to specify that the Workbook should be created "read only" with

Workbook wb = WorkbookFactory.create(myFile, true);

What I'm trying to do is the following:

  • I want to open an existing Workbook from a File;
  • Immediately release any filesystem resources, but retain the Workbook in memory; (this should not update the timestamp of the original file);
  • Make changes to the in-memory Workbook;
  • Save the modified Workbook to a new file.

Is this possible? I've tried many things, but can't seem to get it to work. If I don't want the original file timestamp updated, it seems I have to open the Workbook as read only. But then I can't modify it in memory for a subsequent write to a new file. Catch-22.

[BTW, I'm creating the Workbook from a File rather than a stream, as I understand it is more efficient. If I used a stream I could possibly get around this issue by closing the stream myself. Also, I'm working with XLSX/XLSM not XLS files.]

Thoughts? Thanks!

like image 942
Gurtz Avatar asked Sep 12 '25 04:09

Gurtz


1 Answers

Options could be:

  1. Create a copy of the file and read/write on the copy
  2. Open the original file as read-only, then create a copy to write if required
  3. Use an input stream from the original file to create the workbook
like image 165
rohitvats Avatar answered Sep 13 '25 17:09

rohitvats