本文介绍了通过epplus援引Excel公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Excel工作表中的ASP.NET MVC4 C#项目,我能够从成功地运用EPPlus Excel工作表来读取。现在,我希望能在2号传递到细胞C:2和C:3,并能够在C调用公式:4这是= SUM(C2:C3)。

所以从C#我想在4和6通过并调用式并能够从C得到的结果回:4,其是40(10和30 SUM)。我如何做到在C#。

在以下code,我回去为零d.Average

  d.Average = Convert.ToDouble(currentWorksheet.Cells [C4]值。);

下面是我下面code在C#至今遍历一行。

 使用(VAR包=新ExcelPackage(existingFile))
        {
            ExcelWorkbook工作簿= package.Workbook;
            变种currentWorksheet = workBook.Worksheets.First();
            currentWorksheet.Workbook.CalcMode = ExcelCalcMode.Automatic;
            currentWorksheet.Cells [C4] =公式= SUM(C2:C3);
            currentWorksheet.Cells [C2]值= 10。
            currentWorksheet.Cells [C3]值= 30。
            package.Save();
        }        使用(VAR包=新ExcelPackage(existingFile))
        {
            ExcelWorkbook工作簿= package.Workbook;
            变种currentWorksheet = workBook.Worksheets.First();
            d.Average = Convert.ToDouble(currentWorksheet.Cells [C4]值。);
        }


解决方案

跳过 = 在公式字符串。

替换 currentWorksheet.Cells [C4] =公式= SUM(C2:C3);

currentWorksheet.Cells [C4]公式=SUM(C2:C3);

I have an excel sheet in ASP.NET MVC4 C# project and I am able to read from excel sheet successfully using EPPlus. Now, I want to be able to pass in 2 numbers into cell C:2 and C:3 and be able to invoke formula in C:4 which is =SUM(C2:C3).

So from C# I want to pass in 4 and 6 and invoke the formula and be able to get the result back from C:4 which is 40 (SUM of 10 and 30). How do I accomplish that in C#.

In the following code, I get back zero for d.Average

d.Average = Convert.ToDouble(currentWorksheet.Cells["C4"].Value);

Here is my following code in c# so far to traverse a row.

        using (var package = new ExcelPackage(existingFile))
        {
            ExcelWorkbook workBook = package.Workbook;
            var currentWorksheet = workBook.Worksheets.First();
            currentWorksheet.Workbook.CalcMode = ExcelCalcMode.Automatic;
            currentWorksheet.Cells["C4"].Formula = "=SUM(C2:C3)";
            currentWorksheet.Cells["C2"].Value = 10;
            currentWorksheet.Cells["C3"].Value = 30;
            package.Save();


        }

        using (var package = new ExcelPackage(existingFile))
        {
            ExcelWorkbook workBook = package.Workbook;
            var currentWorksheet = workBook.Worksheets.First();
            d.Average = Convert.ToDouble(currentWorksheet.Cells["C4"].Value);
        }
解决方案

Skip the = in the formula string.

Replace currentWorksheet.Cells["C4"].Formula = "=SUM(C2:C3)";

with

currentWorksheet.Cells["C4"].Formula = "SUM(C2:C3)";

这篇关于通过epplus援引Excel公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 22:06