How to retrieve cell value without enabling sheet calculations in C#, VB.NET?
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 the DisplayText property in the worksheet range.
The below table illustrates the behavior of the DisplayText property with respect to enabling sheet calculations.
| DisplayText while opening an existing workbook | DisplayText while creating a new workbook |
Without EnableSheetCalculation | Always returns the cell value with its number format. | Returns 0, if the cell has a formula Returns cell value with number format, if cell does not have formula |
With EnableSheetCalculation | Returns the evaluated value with number format, if the 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
To know more about enabling and disabling sheet calculations, please refer to the documentation.
The following complete code snippet explains how to retrieve the formula value in a 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
Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through formulas, adding charts in worksheets or workbooks, organizing and analyzing data through tables and pivot tables, appending multiple records to a worksheet using template markers, and most importantly, PDF and image conversions, etc., with code examples.
Refer here to explore the rich set of Syncfusion Essential XlsIO features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to this link to learn about generating and registering a Syncfusion license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to retrieve cell values without enabling sheet calculations in C#, VB.NET.
You can refer to our XlsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums or feedback portal. We are always happy to assist you!