是否可以使用公式读取excel单元格值? [英] Is it possible to read the excel cell value with formula?

查看:68
本文介绍了是否可以使用公式读取excel单元格值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的以下代码中,c4的值为零.C4单元格具有公式SUM(C2:C3).EPPlus能够读取带有公式的单元格吗?为什么将c4设置为零而不是12.

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?
}

推荐答案

从EpPlus 4.0.1.1开始,存在扩展方法 public static void Calculate(此ExcelRangeBase范围).在访问C4的Value属性之前调用它:

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

currentWorksheet.Cells ["C4"].Value 之后将返回 12 .

这篇关于是否可以使用公式读取excel单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆