Skip to content

Commit cd343b9

Browse files
authored
Merge pull request #305 from SyncfusionExamples/992532-FAQFormulaHandling
Documentation(992532): FAQ for how to handle excel formulas stored as strings using cell type validation
2 parents a172ec1 + b6c6a87 commit cd343b9

4 files changed

Lines changed: 71 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="FormulaStoredAsTextHandling/FormulaStoredAsTextHandling.csproj" />
3+
</Solution>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Output\*">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\*">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

‎FAQ/Formula stored as text handling/.NET/FormulaStoredAsTextHandling/FormulaStoredAsTextHandling/Output/.gitkeep‎

Whitespace-only changes.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using Syncfusion.XlsIO;
2+
using Syncfusion.XlsIO.Implementation;
3+
using static Syncfusion.XlsIO.Implementation.WorksheetImpl;
4+
5+
namespace FormulaStoredAsTextHandling
6+
{
7+
class Program
8+
{
9+
public static void Main(string[] args)
10+
{
11+
using (ExcelEngine excelEngine = new ExcelEngine())
12+
{
13+
IApplication application = excelEngine.Excel;
14+
application.DefaultVersion = ExcelVersion.Xlsx;
15+
16+
//Create a new workbook with one worksheet
17+
IWorkbook workbook = application.Workbooks.Create(1);
18+
IWorksheet worksheet = workbook.Worksheets[0];
19+
20+
//Assign a formula as text to the cell
21+
worksheet["A1"].Text = "=SUM(2+2)";
22+
23+
//Get the cell type of
24+
Syncfusion.XlsIO.Implementation.WorksheetImpl.TRangeValueType cellType =
25+
(worksheet as WorksheetImpl).GetCellType(1, 1, false);
26+
27+
//Check if the cell type is string
28+
if (cellType == TRangeValueType.String)
29+
{
30+
//Retrieve the formula text
31+
string formulaText = worksheet["A1"].Text;
32+
33+
//Clear the cell value
34+
worksheet["A1"].Value = string.Empty;
35+
36+
//Reassign the formula to the cell
37+
worksheet["A1"].Formula = formulaText;
38+
}
39+
40+
//Save the workbook
41+
workbook.SaveAs("../../../Output/Output.xlsx");
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)