c# npoi excel How to get a cell's formula VALUE?

14,492

Solution 1

HSSFFormulaEvaluator formula = new HSSFFormulaEvaluator(workBook);

then you can use this formula for all cells in your excel file by using

formula.EvaluateAll();

or you can use it for specific cell like this

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

Solution 2

Try to use:

another.GetRow(0).GetCell(28).NumericCellValue;

You can use several different properties based on column type.

Solution 3

Use StringCellValue to get the value from cell your formula will not read

MessageBox.Show(another.GetRow(0).GetCell(28).StringCellValue

Solution 4

Use "EvaluateAllFormulaCells(workbook)" method to evaluate all formulas after setting it into excel file. see this link How to re-calculate a cell's formula?

Share:
14,492

Related videos on Youtube

user7099027
Author by

user7099027

Updated on June 04, 2022

Comments

  • user7099027
    user7099027 almost 2 years

    I set a formula on some cells in a loop like this:

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

    I always wondered how to get the result (value) of this formula and not copy the entire formula.

    MessageBox.Show(another.GetRow(0).GetCell(28).ToString);
    

    it dislplay value IF(B2,J2=J1,FALSE)

    how can get the result (value) not formula?

  • user7099027
    user7099027 about 7 years
    cannot work, it display value 0,but in excel dispaly FALSE
  • Przemysław Kleszcz
    Przemysław Kleszcz about 7 years
    So try to use BooleanCellValue property