c#npoi excel 如何获取单元格的公式值?

use*_*027 7 c# excel npoi

我在循环中的某些单元格上设置了一个公式,如下所示:

System.String fm = "IF(B2,J2=J1,FALSE)"
another.GetRow(0).CreateCell(28).SetCellFormula(fm); 
Run Code Online (Sandbox Code Playgroud)

我一直想知道如何获得这个公式的结果(值)而不是复制整个公式。

MessageBox.Show(another.GetRow(0).GetCell(28).ToString);
Run Code Online (Sandbox Code Playgroud)

它显示值 IF(B2,J2=J1,FALSE)

如何得到结果(值)而不是公式?

小智 5

HSSFFormulaEvaluator formula = new HSSFFormulaEvaluator(workBook);
Run Code Online (Sandbox Code Playgroud)

然后你可以使用这个公式在你的excel文件中的所有单元格中使用

formula.EvaluateAll();
Run Code Online (Sandbox Code Playgroud)

或者您可以将它用于这样的特定单元格

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;
                }
            }
Run Code Online (Sandbox Code Playgroud)


Prz*_*zcz 4

尝试使用:

another.GetRow(0).GetCell(28).NumericCellValue;
Run Code Online (Sandbox Code Playgroud)

您可以根据列类型使用多种不同的属性。

  • 所以尝试使用 BooleanCellValue 属性 (2认同)