Articles in this section
Category / Section

How to retrieve cell value without enabling sheet calculations in C#, VB.NET?

1 min read

This article explains how to retrieve the cell value without enabling sheet calculations using XlsIO.

There can be situations where the formula value or the last calculated value in a cell is required without actually recalculating sheet formulas through EnableSheetCalculations. This can be achieved using DisplayText property in worksheet range.

The below table illustrates the behavior of DisplayText property with respect to enabling sheet calculations.

 

DisplayText while opening an existing workbook

DisplayText while creating a new workbook

Without EnableSheetCalculation

Always returns cell value with its number format.

Returns 0, if cell has a formula

Returns cell value with number format, if cell does not have formula

With EnableSheetCalculation

Returns evaluated value with number format, if cell has a formula

Returns cell value with number format, if cell does not have formula

Returns evaluated value with number format, if cell has a formula

 

Code snippet to use DisplayText property

//Retrieving display text of the cell 
string displayText = worksheet.Range["C2"].DisplayText;

Download input sample with data

Download complete sample

To know more about enabling and disabling sheet calculations, please refer the documentation.

The following complete code snippet explains how to retrieve the formula value in cell without enabling sheet calculations using XlsIO.

C#

using Syncfusion.XlsIO;
using System.IO;
using System.Reflection;
 
namespace FormulaValue
{
    class Program
    {
        static void Main(string[] args)
        {
            using (ExcelEngine excelEngine = new ExcelEngine())
            {
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Excel2013;
 
                //Opening existing workbook with named range
                Assembly assembly = typeof(Program).GetTypeInfo().Assembly;
                Stream inputStream = assembly.GetManifestResourceStream("FormulaValue.Sample.xlsx");
                IWorkbook workbook = application.Workbooks.Open(inputStream);
               IWorksheet worksheet = workbook.Worksheets[0];
 
               //Retrieving formula in the cell using Formula
               string formula = worksheet.Range["C2"].Formula;
 
               //Retrieving display text of the cell 
               string displayText = worksheet.Range["C2"].DisplayText;
 
               //Saving the workbook as stream
               Stream outputStream = File.Create("Output.xlsx");
                workbook.SaveAs(outputStream);
            }
        }
    }
}

VB

Imports System.IO
Imports System.Reflection
Imports Syncfusion.XlsIO
 
Namespace FormulaValue
        Class Program
           Public Shared Sub Main(ByVal args() As String)
                   Using excelEngine As ExcelEngine = New ExcelEngine
                        Dim application As IApplication = excelEngine.Excel
                        application.DefaultVersion = ExcelVersion.Excel2013
 
                        'Opening existing workbook with named range
                        Dim assembly As Assembly = GetType(Program).GetTypeInfo.Assembly
                        Dim inputStream As Stream = assembly.GetManifestResourceStream("FormulaValue.Sample.xlsx")
                        Dim workbook As IWorkbook = application.Workbooks.Open(inputStream)
                        Dim worksheet As IWorksheet = workbook.Worksheets(0)
 
                        'Retrieving formula in the cell using Formula
                        Dim formula As String = worksheet.Range("C2").Formula
 
                        'Retrieving display text of the cell
                        Dim displayText As String = worksheet.Range("C2").DisplayText
 
                        'Saving the workbook as stream
                        Dim outputStream As Stream = File.Create("Output.xlsx")
                        workbook.SaveAs(outputStream)
               End Using
            End Sub
        End Class
End Namespace

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied