0

i have a very simple c# app which uses EPPlus 4.1.1 to modify a simple excel spreadsheet file:

FileInfo fileInfo = new FileInfo(@"D:\file.xlsx");
ExcelPackage package = new ExcelPackage(fileInfo);
ExcelWorksheet workSheet = package.Workbook.Worksheets["Sheet1"];

    // mod logic

package.Save();

everything seems to work swimmingly. once my app completes its execution, i open the modified spreadsheet file from the file browser via Excel to confirm that modifications were executed properly. the problem is that once i attempt to close Excel, it prompts me to save. Now, whether i save or dont, the modifications remain. Yet Excel continues to prompt for save until i choose to save. i take it that perhaps there is a missing step or two in the code thats causing this. any ideas? thank you!

user1056027
  • 145
  • 2
  • 2
  • 13
  • 1
    This is fairly well known behaviour - there may be expressions in cells which cause a recalculation when the spreadsheet is opened - which gets flagged as a change despite you doing nothing. See here for a bit more info : https://superuser.com/questions/464585/why-does-this-excel-file-keep-asking-to-save and/or google something like "excel prompts to save when no changes " – PaulF Mar 06 '18 at 17:18

1 Answers1

0

Some users have this problem because of the nature of how EPPlus creates the XML files. As I understand it, the Excel files created by EPPlus may be incomplete in Excel's eyes, and therefore Excel's "save" function is attempting to "fill in" the missing parts of the underlying XML.

In my own experience, I have found that sometimes adding a recalculate before I save the file puts things in a sufficiently stable state that Excel does not attempt to save again upon a simple open/look/close cycle.

I have also experienced situations where some of my edits in EPPlus create a logically unstable worksheet that Excel has to repair; most of the time these are logic errors on my part, things like merging cells and then grouping or hiding certain parts. Fixing my logic resolved these errors.

I found this answer from Rÿck Nöthing who experienced a similar problem. Personally I would remove the "FullCalcOnLoad" statement, as your needs may differ from his and you may want a full recalc of the spreadsheet on load.

I ran into this same issue using EPPlus (version 4.1.0, fyi) and found adding the following code before closing fixed the problem:

p.Workbook.Calculate();
p.Workbook.FullCalcOnLoad = false;