3

See this .xlsx file. No macros, of course!

When you open the file, you will see the following cells:

[Series     ]
[10         ]
[20         ]
[30         ]
[           ]
[           ]
[=SUM(B3:B7)]

The sum does not calculate unless you double click, and then press enter, in one of the bottom three cells (eg two blank, =SUM(B3:B7))


Weirder still, once you 'kicked the formula into life' if you save the workbook and reopen it, it behaves fine.

I've tried changing the extension to .zip, and playing around with Xml to see if I could identify the changes made by saving it after it's started working, but I could only see that a version number had changed.

We. Ird.


Also, I tried saving this in an earlier Excel format, and the problem remained.


I just want to reiterate - the attached spreadsheet is a .xlsx file, and therefore does not have any macros! There really is no other way of explaining this problem without the attached spreadsheet. Hopefully, by putting 50 points of reputation on the line, someone will trust that I'm not a script kiddie and have a look!


I created the file using EPPlus, and the following C# code:

    public void Generate(string outputPath)
    {
        using (FileStream fsTemplate = new FileStream(TemplatePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            using (FileStream fsReport = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
            {
                using (ExcelPackage ep = new ExcelPackage(fsReport, fsTemplate))
                {
                    using (ExcelWorksheet ws = ep.Workbook.Worksheets[TemplateSheetName])
                    {
                        //
                        int iHeadingsRow = 2;
                        int dataRowIndex = iHeadingsRow + 1;
                        //
                        List<decimal> list = new List<decimal>() { 10, 20, 30 };
                        //
                        foreach (var number in list)
                        {
                            ws.Cells[dataRowIndex, 2].Value = list[dataRowIndex - iHeadingsRow - 1];
                            //
                            ws.InsertRow(dataRowIndex + 1, 1, iHeadingsRow + 1);
                            //
                            dataRowIndex++;
                        }
                        //
                        ep.Save();
                    }
                }
            }
        }
    }

I would like to emphasise that this is not an EPPlus issue! This is a common problem with Excel opening a valid workbook, and refusing to calculate a formula. Formatting the cells as numeric makes no difference. I have been using EPPlus to create working Excel files for months, and I have not installed any updates to EPPlus, so it must be problem with Excel, right?!

The solution must lie in the difference between the original file, and the version that has been kicked into life and then saved. I just can't seem to pinpoint exactly what that difference is...


The =SUM(B3:B7) is already present in the template file, along with the text 'Series'. The code only adds the {10,20,30} items into the spreadsheet.

  • 4
    Is calculation set to automatic? [Possible duplicate of this thread](https://superuser.com/questions/836324/cells-not-updating-automatically). – BruceWayne Apr 05 '18 at 15:40
  • 1
    Nope! Calculation is set to auto. Please try the spreadsheet and you will see what I mean! – 3-14159265358979323846264 Apr 05 '18 at 19:15
  • 1
    Sorry, as a rule I (and I'm sure most others) don't download misc. files from online, especially if/when macros could be involved. If you can add more detail or information to your OP, it would help. – BruceWayne Apr 05 '18 at 19:24
  • 2
    I'm afraid there's nothing else I can add. This is one of those 'must see to believe' situations. Also, it's not a .xlsm file, so no macros, and you're more than welcome to run the file through jotti or virustotal if you're concerned about potential viruses. There are plenty of instances on forums of people having this problem, so i created the minimal, verifiable example of the problem, and provided a link to that because there really is no other way to ask this question. Thanks for your help :0) – 3-14159265358979323846264 Apr 06 '18 at 10:32

1 Answers1

1

You are adding data to an already existing Excel sheet which already contains the =SUM(B3:B7) formula.

I guess that is an EPPlus issue. Namely, the issue is that you are not calling the Calculate() method after entering the data:

... you can let EPPlus calculate the results of the formulas in a workbook.

This is done by calling the Calculate() method, which is available on Workbook, Worksheet and Range level. When Calculate() is called EPPlus will evaluate the result of the formula and store the result as the Value of the cell - just like Excel do.

After you type a formula in Excel, Excel calculates the value and stores it in the file together with the value.

For example you have a sheet like:

100
200
=SUM(A1:A2)

Excel stores this (I removed the non-relevant XML attributes):

<sheetData>
    <row r="1">
        <c r="A1">
            <v>100</v>
        </c>
    </row>
    <row r="2">
        <c r="A2">
            <v>200</v>
        </c>
    </row>
    <row r="3">
        <c r="A3">
            <f>SUM(A1:A2)</f>
            <v>300</v></c>
    </row>
</sheetData>

When you insert the data to the XLSX file externally without the value, Excel does not have the cached value stored yet and displays 0. Your XLSX file (the one which you uploaded) contains in sheet1.xml:

<row r="8">
    <c r="B8">
        <f>SUM(B3:B7)</f>
        <v>0</v>
    </c>
</row>

There is some explanation in the similar Java library Apache POI:

Sometimes Excel will notice itself, and trigger a recalculation on load, but unless you know you are using volatile functions it's generally best to trigger a Recalculation.

and there is a similar problem Excel cell displaying zero instead of a calculated / referenced value from the Java perspective.

Community
  • 1
  • 1
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • Ok. I understand that EPPlus has a calculate method, but my question is "why won't Excel calculate the formula until i double click one of the blank cells". That's an issue with Excel, not EPPlus! :0) If autocalculate is on, Excel should update all the formulas when the workbook is opened. – 3-14159265358979323846264 Apr 09 '18 at 11:32
  • Ah. Ok. So that's the difference then! The cached value isn't present. It all makes sense now :0) What's strange is that the cached value must get stored in some circumstances, even without calling the `calculate()` method, because some of my templates don't suffer from this problem, and some do. I think that's down to them being created in a slightly different version of Excel - older files don't require the `calculate()` method to be called, but they still display the correct total. – 3-14159265358979323846264 Apr 09 '18 at 11:44
  • In any case, add the `EPPlus` tag to your question :) – Honza Zidek Apr 09 '18 at 11:46
  • 1
    I really wanted to know why Excel behaves in this weird way (requiring a double click on the blank cell before the formula is calculated), but i guess that going to be down to some internal implementation. You have earned your 50 bonus points! Thanks again for your insight :0) Apparently, I have to wait 22 hours to award the bounty! – 3-14159265358979323846264 Apr 09 '18 at 11:48