Search code examples
c#excelepplus

Formula not calculating with EPPLUS


I would like to transfer a Datatableto excel and calculate a sum. For this I'm using epplus. Here is my code:

Sheet.Cells["A1"].LoadFromDataTable(dsExcel.Tables[0], true, TableStyles.Medium9);
Sheet.Cells[Sheet.Dimension.Address].AutoFitColumns();
Sheet.View.FreezePanes(3, 3);

int totalCols = Sheet.Dimension.End.Column;
int letztezeile = Sheet.Dimension.End.Row;
var range = Sheet.Cells[1, 1, 1, totalCols];

for (int i = 3; i < totalCols; i++)
{
   if (range[1, i].Value.ToString().Contains("Durchfluss")) Sheet.Column(i).Style.Numberformat.Format = "#,##0.00";
 }

 Sheet.InsertRow(2, 1);
 Sheet.Cells["A2"].Value = "Summe";
 for (int i = 4; i <= totalCols; i++)
 {
     Sheet.Cells[2, i, 2, i].Formula = "SUMME(" + range[3, i].Address + ":" + range[letztezeile + 1, i].Address + ")";
 }

This is my output:

enter image description here

I have to enter the cell D2 and press Enter, then the formula is working:

enter image description here

So what must I change that the formula is working from the beginning? Thanks


Solution

  • You should change "SUMME" to "SUM", localized formula names is handled by the Excel GUI. The underlying xml (same as Excel produces) assumes that the formula names are in english.

    From EPPlus Formula calc documentation:

    • Don't use localized function names. Only english names (such as SUM, IF, VLOOKUP, etc) are supported.
    • Don't use semicolon as a separator between function arguments. Only comma is supported.
    • Don't add the leading = sign in your formula. "=SUM(A1:A2)" is wrong, "SUM(A1:A2)" is correct