EPPlus icon indicating copy to clipboard operation
EPPlus copied to clipboard

Improvement idea for ExcelPackage.Save

Open curtis1757 opened this issue 5 years ago • 2 comments
trafficstars

Suggestion: Currently ExcelPackage.Save deletes the original .xls file, then (tries to) write the new one. If there are any issues in writing the new file the old original file is left deleted. I would suggest either first renaming the original .xls file ( File.FullName) to some temporary file name, writing the new .xls file with the name File.FullName then upon successful writing delete the old original renamed .xls file. If there were any problems then delete the bad file written (if any) and rename the original .xls file back to File.FullName ...or... Simply write the new .xls file with a temporary file name, then upon success, delete original .xls file (File.FullName) and then rename the new .xls file to File.FullName.

This has not caught me as a problem as I always have a backup, but just seems like it would be good not to delete the original file until the new/updated file has been successfully written.

Again, thanks for a great software package, very useful! Curtis

curtis1757 avatar Jul 13 '20 06:07 curtis1757

Thanks Curtis, we will consider this!

swmal avatar Jul 15 '20 06:07 swmal

When saving in a web application like ASP.Net, is it thread-safe? Or should we do locking with either a mutex or classic lock object, either for the line that opens the file, or perhaps only for the .save?

I have 1 workbook file that the application will mostly read from, and perform calculations, but won't save. But there will be a few places in the application where the user can provide some new values that need to be saved to this same file. After saving, the next time the file is read from, the new values need to be there. There's multiple concurrent users getting data from the file, but less than 100.

Thanks.

Dean-NC avatar Aug 28 '20 17:08 Dean-NC

A actually got real problem with this) It was unobvoius, if you try to save a file too fast twice, it has a chanse to throw InvalidOperationException("Part does not exist."); from ZipPacagePart.GetPart(string part uri), i think this needs to be fixed, steps to reproduce are simple 1)adding about thousand of text string in worksheet 2)Save 3)adding another thousan 4)Save Boom - exception Unfortunately right now i can't bur report this, but it could lead to real problems, i lost dialogue dataset for nlp task for example

SamoVariwe avatar Oct 28 '23 02:10 SamoVariwe

Save will overwrite the existing package. This is by design. Use the SaveAs method or make a copy of the workbook before opening the package, if there are concerns about losing data.

JanKallman avatar Feb 20 '24 13:02 JanKallman

@JanKallman I still think this is an issue, as Save should not be deleting the original file. If this is by design, I think maybe the design should be changed.

What are the potential problems of changing the Save design?

Thanks, Curtis

curtis1757 avatar Feb 20 '24 20:02 curtis1757

@curtis1757 Keeping the file will add extra cost to the save operation, doubling the amount of disk space used to save a package and add extra IO operations as well. The Save method will overwrite the file, while the SaveAs method will save the package to a new file. The Save method should not fail. If it does, we should address that issue and fix it.

JanKallman avatar Feb 21 '24 07:02 JanKallman

I understand. But the 'overwrite' method used is not to open and update the existing file, but to first completely delete it, then later write a new file, albeit, later is very short time. :-)

Thanks for the update!

curtis1757 avatar Feb 21 '24 07:02 curtis1757