EPPlus 计算不起作用

EPPlus calculation not working

我正在使用 C# 和 EPPlus 来计算一堆总和。我正在尝试将公式应用于一行以及一堆单个单元格。但是,我目前的方法不是计算公式。当我使用值方法时,它只是将值作为字符串发布并且不计算任何一个。

//=SUM(E123+G123+H123+I123) -F123
//=SUMIFS($P:$P00,$C:$C00,Q2,$A:$A00, $R2)

string formula = "= 2 *2";
            //double f = double.Parse(formula);
       // ws.Cells["q4"].Value = formula;
       ws.Cells["q4"].Formula =formula;
        ws.Cells["q4"].Style.Numberformat.Format = "#,##0";
        ws.Workbook.CalcMode = ExcelCalcMode.Automatic;
        ws.Cells["q4"].Calculate();


        if (ws.Cells["q4"].Value.ToString() != null)
        {
            string test = ws.Cells["q4"].Value.ToString();
            MessageBox.Show("the value :" + test);
        }
        else
        {
            MessageBox.Show("not working" );
        }

我查了你给的样本,公式是2 * 2,EPPLus好像是:

string formula = "= 2 *2";

实际上必须是:

string formula = "2 *2";

即没有等号。请参阅下面我对您的示例的修改:

//  Just setting up a dummy package here so I can test your example
var pckg = new ExcelPackage();
pckg.Workbook.Worksheets.Add("Test");
var ws = pckg.Workbook.Worksheets.First(w => w.Name == "Test");

//  Now that I have a dummy worksheet, I run your code.
string formula = "2 *2"; //  Note I dropped the equal sign here from your original example
ws.Cells["q4"].Formula = formula;
ws.Cells["q4"].Style.Numberformat.Format = "#,##0";
ws.Workbook.CalcMode = ExcelCalcMode.Automatic;
ws.Cells["q4"].Calculate();

if (ws.Cells["q4"].Value.ToString() != null)
{
    string test = ws.Cells["q4"].Value.ToString();
    MessageBox.Show("the value :" + test); //  test = 4
}
else
{
    MessageBox.Show("not working" );
}

但我认为 EPPLus documentation 如果您加载的工作表的单元格中有公式,您不必担心等号。