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)";