问题
In my following code, the value of c4 is coming out zero. C4 cell has formula SUM(C2:C3). Is EPPlus capable of reading cell with formula? Why is c4 being set to zero instead of 12.
using (var package = new ExcelPackage(existingFile))
{
ExcelWorkbook workBook = package.Workbook;
var currentWorksheet = workBook.Worksheets.First();
currentWorksheet.Cells["C2"].Value = 5;
currentWorksheet.Cells["C3"].Value = 7;
var c4 = Convert.ToDouble(currentWorksheet.Cells["C4"].Value); //c4 is zero. why?
}
回答1:
As of EpPlus 4.0.1.1, there is an extension method public static void Calculate(this ExcelRangeBase range)
. Invoke it prior to accessing C4's Value property:
currentWorksheet.Cells["C4"].Calculate();
and currentWorksheet.Cells["C4"].Value
will return 12
after that.
来源:https://stackoverflow.com/questions/21213106/is-it-possible-to-read-the-excel-cell-value-with-formula