0
votes

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

2
Looks like it should work. Does Excel provide you an error message when you hover over it? - Ernie S
@Ernie it says "text argument cannot be interpreted" but in my code I change the column to numberfomat. The input is all double so there should be no problem - Kᴀτᴢ

2 Answers

7
votes

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
0
votes

After searching long time it seems that epplus ignores the datatype if I use .LoadFromDataTable and write every value as string. So I have to parse every value to double and write it back into the cells. After that everything is running fine.

double ZahlAlsText;
        for (int i = 2; i < totalCols; i++) 'columns
        {
            for (int y = 2; y <= letztezeile + 1; y++) 'rows
            {
                ZahlAlsText = double.Parse(range[y, i].Value.ToString());
                range[y, i].Style.Numberformat.Format = "#,##0.00";
                range[y, i].Value = ZahlAlsText;
            }
        }