Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Document-Processing-toc.html
Original file line number Diff line number Diff line change
Expand Up @@ -6488,6 +6488,9 @@
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-get-the-rgb-color-value-for-the-applied-cell-color">How to get the RGB color value for the applied cell color?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-restore-formulas-stored-as-text-in-excel-documents">How to restore formulas stored as text in Excel documents?</a>
</li>
<li>
<a href="/document-processing/excel/excel-library/net/faqs/how-to-save-the-edited-chages-in-the-same-excel-document">How to save the edited changes in the same Excel document?</a>
</li>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
---
title: Restore Excel formulas stored as text | Syncfusion
description: Code example to restore Excel formulas stored as text in Excel documents using Syncfusion .NET Excel library (XlsIO).
platform: document-processing
control: XlsIO
documentation: UG
---

# How to restore formulas stored as text in Excel documents?

The following code examples demonstrate how to handle Excel formulas stored as strings using cell type validation using C# (Cross-platform and Windows-specific) and VB.NET.

{% tabs %}
{% highlight c# tabtitle="C# [Cross-platform]" playgroundButtonLink="https://raw.githubusercontent.com/SyncfusionExamples/XlsIO-Examples/master/FAQ/Formula%20stored%20as%20text%20handling/.NET/FormulaStoredAsTextHandling/FormulaStoredAsTextHandling/Program.cs,180" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Create a new workbook with one worksheet
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

//Assign a formula as text to the cell
worksheet["A1"].Text = "=SUM(2+2)";

//Get the cell type of the cell
Syncfusion.XlsIO.Implementation.WorksheetImpl.TRangeValueType cellType =
(worksheet as WorksheetImpl).GetCellType(1, 1, false);

//Check if the cell type is string
if (cellType == TRangeValueType.String)
{
//Retrieve the formula text
string formulaText = worksheet["A1"].Text;

//Clear the cell value
worksheet["A1"].Value = string.Empty;

//Reassign the formula to the cell
worksheet["A1"].Formula = formulaText;
}

//Save the workbook
workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx"));
}
{% endhighlight %}

{% highlight c# tabtitle="C# [Windows-specific]" %}
using (ExcelEngine excelEngine = new ExcelEngine())
{
IApplication application = excelEngine.Excel;
application.DefaultVersion = ExcelVersion.Xlsx;

//Create a new workbook with one worksheet
IWorkbook workbook = application.Workbooks.Create(1);
IWorksheet worksheet = workbook.Worksheets[0];

//Assign a formula as text to the cell
worksheet["A1"].Text = "=SUM(2+2)";

//Get the cell type of the cell
Syncfusion.XlsIO.Implementation.WorksheetImpl.TRangeValueType cellType =
(worksheet as WorksheetImpl).GetCellType(1, 1, false);

//Check if the cell type is string
if (cellType == TRangeValueType.String)
{
//Retrieve the formula text
string formulaText = worksheet["A1"].Text;

//Clear the cell value
worksheet["A1"].Value = string.Empty;

//Reassign the formula to the cell
worksheet["A1"].Formula = formulaText;
}

//Save the workbook
workbook.SaveAs("Output.xlsx");
}
{% endhighlight %}

{% highlight vb.net tabtitle="VB.NET [Windows-specific]" %}
Using excelEngine As New ExcelEngine()
Dim application As IApplication = excelEngine.Excel
application.DefaultVersion = ExcelVersion.Xlsx

'Create a new workbook with one worksheet
Dim workbook As IWorkbook = application.Workbooks.Create(1)
Dim worksheet As IWorksheet = workbook.Worksheets(0)

'Assign a formula as text to the cell
worksheet("A1").Text = "=SUM(2+2)"

'Get the cell type
Dim cellType As Syncfusion.XlsIO.Implementation.WorksheetImpl.TRangeValueType =
CType(worksheet, WorksheetImpl).GetCellType(1, 1, False)

'Check if the cell type is string
If cellType = TRangeValueType.String Then
'Retrieve the formula text
Dim formulaText As String = worksheet("A1").Text

'Clear the cell value
worksheet("A1").Value = String.Empty

'Reassign the formula to the cell
worksheet("A1").Formula = formulaText
End If

'Save the workbook
workbook.SaveAs(IO.Path.GetFullPath("Output/Output.xlsx"))
End Using
{% endhighlight %}
{% endtabs %}

A complete working example in C# is present on <a href="https://github.com/SyncfusionExamples/XlsIO-Examples/tree/master/FAQ/Formula%20stored%20as%20text%20handling/.NET/FormulaStoredAsTextHandling">this GitHub page</a>.