我在循环中的一些单元格上设置了一个公式,如下所示:
System.String fm = "IF(B2,J2=J1,FALSE)"
another.GetRow(0).CreateCell(28).SetCellFormula(fm);
我一直想知道如何得到这个公式的结果(值)而不是复制整个公式。
MessageBox.Show(another.GetRow(0).GetCell(28).ToString);
它显示值 IF(B2,J2=J1,FALSE)
如何得到结果(值),而不是公式本身?
最佳答案
HSSFFormulaEvaluator formula = new HSSFFormulaEvaluator(workBook);
然后您可以将此公式用于 excel 文件中的所有单元格,方法是使用
formula.EvaluateAll();
或者你可以像这样将它用于特定的单元格
var cell = sheet.GetRow(row).GetCell(column);
string Res = "";
if (cell != null)
{
formula.EvaluateInCell(cell);
switch (cell.CellType)
{
case NPOI.SS.UserModel.CellType.Numeric:
Res = sheet.GetRow(row).GetCell(column).NumericCellValue.ToString();
break;
case NPOI.SS.UserModel.CellType.String:
Res = sheet.GetRow(row).GetCell(column).StringCellValue;
break;
}
}
关于c# npoi excel 如何获取单元格的公式值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42971756/