Formula not calculating with EPPLUS

喜欢而已 提交于 2020-12-27 06:48:33

问题


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


回答1:


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



回答2:


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;
            }
        }


来源:https://stackoverflow.com/questions/31513560/formula-not-calculating-with-epplus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!