EPPlus计算不起作用

MJC

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

//=SUM(E123+G123+H123+I123) -F123
//=SUMIFS($P$4:$P$1000,$C$4:$C$1000,Q2,$A$4:$A$1000, $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文档中认为,如果加载的工作表的单元格中有公式,则不必担心等号。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章