Skip to content

Commit 0a90a7f

Browse files
authored
Merge pull request #257 from SyncfusionExamples/969014-video
969014 - How to Work with Formulas in an Excel Document Using the .NET Excel Library
2 parents 1be6799 + a62a2ca commit 0a90a7f

File tree

75 files changed

+74910
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+74910
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
using System.Diagnostics;
2+
using Excel_Formulas.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Syncfusion.XlsIO;
5+
6+
namespace Excel_Formulas.Controllers
7+
{
8+
public class HomeController : Controller
9+
{
10+
private readonly ILogger<HomeController> _logger;
11+
12+
public HomeController(ILogger<HomeController> logger)
13+
{
14+
_logger = logger;
15+
}
16+
17+
public IActionResult AddFormula()
18+
{
19+
ExcelEngine excelEngine = new ExcelEngine();
20+
IApplication application = excelEngine.Excel;
21+
application.DefaultVersion = ExcelVersion.Xlsx;
22+
IWorkbook workbook = application.Workbooks.Create(1);
23+
IWorksheet worksheet = workbook.Worksheets[0];
24+
25+
worksheet.Range["A1"].Number = 10;
26+
worksheet.Range["B1"].Number = 20;
27+
28+
worksheet.Range["C1"].Formula = "=SUM(A1,B1)";
29+
30+
return ExportWorkbook(workbook, "Formula.xlsx");
31+
}
32+
33+
public IActionResult CrossSheetReference()
34+
{
35+
ExcelEngine excelEngine = new ExcelEngine();
36+
IApplication application = excelEngine.Excel;
37+
application.DefaultVersion = ExcelVersion.Xlsx;
38+
IWorkbook workbook = application.Workbooks.Create(2);
39+
IWorksheet worksheet1 = workbook.Worksheets[0];
40+
IWorksheet worksheet2 = workbook.Worksheets[1];
41+
42+
worksheet1.Range["A1"].Number = 10;
43+
worksheet2.Range["B1"].Number = 20;
44+
45+
worksheet1.Range["C1"].Formula = "=SUM(Sheet2!B1,Sheet1!A1)";
46+
47+
return ExportWorkbook(workbook, "CrossSheetReference.xlsx");
48+
}
49+
50+
public IActionResult NamedRanges()
51+
{
52+
ExcelEngine excelEngine = new ExcelEngine();
53+
IApplication application = excelEngine.Excel;
54+
application.DefaultVersion = ExcelVersion.Xlsx;
55+
IWorkbook workbook = application.Workbooks.Create(1);
56+
IWorksheet worksheet = workbook.Worksheets[0];
57+
58+
worksheet.Range["A1"].Number = 10;
59+
worksheet.Range["B1"].Number = 20;
60+
61+
IName name1 = workbook.Names.Add("One");
62+
name1.RefersToRange = worksheet.Range["A1"];
63+
64+
IName name2 = workbook.Names.Add("Two");
65+
name2.RefersToRange = worksheet.Range["B1"];
66+
67+
worksheet.Range["C1"].Formula = "=SUM(One,Two)";
68+
69+
return ExportWorkbook(workbook, "NamedRanges.xlsx");
70+
}
71+
72+
public IActionResult ArrayFormula()
73+
{
74+
ExcelEngine excelEngine = new ExcelEngine();
75+
IApplication application = excelEngine.Excel;
76+
application.DefaultVersion = ExcelVersion.Xlsx;
77+
IWorkbook workbook = application.Workbooks.Create(1);
78+
IWorksheet worksheet = workbook.Worksheets[0];
79+
80+
worksheet.Range["A1:D1"].FormulaArray = "{1,2,3,4}";
81+
82+
worksheet.Names.Add("ArrayRange", worksheet.Range["A1:D1"]);
83+
84+
worksheet.Range["A2:D2"].FormulaArray = "ArrayRange+100";
85+
86+
return ExportWorkbook(workbook, "ArrayFormula.xlsx");
87+
}
88+
89+
public IActionResult TableFormula()
90+
{
91+
ExcelEngine excelEngine = new ExcelEngine();
92+
IApplication application = excelEngine.Excel;
93+
application.DefaultVersion = ExcelVersion.Xlsx;
94+
IWorkbook workbook = application.Workbooks.Create(1);
95+
IWorksheet worksheet = workbook.Worksheets[0];
96+
97+
IListObject table = worksheet.ListObjects.Create("Table1", worksheet["A1:D3"]);
98+
99+
worksheet[1, 1].Text = "Products";
100+
worksheet[1, 2].Text = "Rate";
101+
worksheet[1, 3].Text = "Quantity";
102+
worksheet[1, 4].Text = "Total";
103+
104+
worksheet[2, 1].Text = "Item1";
105+
worksheet[2, 2].Number = 200;
106+
worksheet[2, 3].Number = 2;
107+
108+
worksheet[3, 1].Text = "Item2";
109+
worksheet[3, 2].Number = 300;
110+
worksheet[3, 3].Number = 3;
111+
112+
table.Columns[3].CalculatedFormula = "SUM(20,[Rate]*[Quantity])";
113+
114+
return ExportWorkbook(workbook, "CalculatedColumn.xlsx");
115+
}
116+
117+
private FileStreamResult ExportWorkbook(IWorkbook workbook, string fileName)
118+
{
119+
MemoryStream stream = new MemoryStream();
120+
workbook.SaveAs(stream);
121+
stream.Position = 0;
122+
return File(stream, "application/xlsx", fileName);
123+
}
124+
125+
public IActionResult Index()
126+
{
127+
return View();
128+
}
129+
130+
public IActionResult Privacy()
131+
{
132+
return View();
133+
}
134+
135+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
136+
public IActionResult Error()
137+
{
138+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
139+
}
140+
}
141+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="31.1.17" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Excel_Formulas.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/Excel_Formulas/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Excel_Formulas
2+
{
3+
public class Program
4+
{
5+
public static void Main(string[] args)
6+
{
7+
var builder = WebApplication.CreateBuilder(args);
8+
9+
// Add services to the container.
10+
builder.Services.AddControllersWithViews();
11+
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
16+
{
17+
app.UseExceptionHandler("/Home/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
app.UseStaticFiles();
24+
25+
app.UseRouting();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllerRoute(
30+
name: "default",
31+
pattern: "{controller=Home}/{action=Index}/{id?}");
32+
33+
app.Run();
34+
}
35+
}
36+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:46852",
8+
"sslPort": 44381
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5287",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7138;http://localhost:5287",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px"> Working with Formulas in Excel</h2>
7+
<div>
8+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
9+
onclick="location.href='@Url.Action("AddFormula", "Home")'">
10+
Add Formula
11+
</button>
12+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
13+
onclick="location.href='@Url.Action("CrossSheetReference", "Home")'">
14+
Cross-Sheet Reference
15+
</button>
16+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
17+
onclick="location.href='@Url.Action("NamedRanges", "Home")'">
18+
Named Ranges
19+
</button>
20+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
21+
onclick="location.href='@Url.Action("ArrayFormula", "Home")'">
22+
Array Formula
23+
</button>
24+
<button style="width: 250px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
25+
onclick="location.href='@Url.Action("TableFormula", "Home")'">
26+
Table Formula
27+
</button>
28+
</div>
29+
</div>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@{
2+
ViewData["Title"] = "Privacy Policy";
3+
}
4+
<h1>@ViewData["Title"]</h1>
5+
6+
<p>Use this page to detail your site's privacy policy.</p>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
@model ErrorViewModel
2+
@{
3+
ViewData["Title"] = "Error";
4+
}
5+
6+
<h1 class="text-danger">Error.</h1>
7+
<h2 class="text-danger">An error occurred while processing your request.</h2>
8+
9+
@if (Model.ShowRequestId)
10+
{
11+
<p>
12+
<strong>Request ID:</strong> <code>@Model.RequestId</code>
13+
</p>
14+
}
15+
16+
<h3>Development Mode</h3>
17+
<p>
18+
Swapping to <strong>Development</strong> environment will display more detailed information about the error that occurred.
19+
</p>
20+
<p>
21+
<strong>The Development environment shouldn't be enabled for deployed applications.</strong>
22+
It can result in displaying sensitive information from exceptions to end users.
23+
For local debugging, enable the <strong>Development</strong> environment by setting the <strong>ASPNETCORE_ENVIRONMENT</strong> environment variable to <strong>Development</strong>
24+
and restarting the app.
25+
</p>
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>@ViewData["Title"] - Excel_Formulas</title>
7+
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
8+
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
9+
<link rel="stylesheet" href="~/Excel_Formulas.styles.css" asp-append-version="true" />
10+
</head>
11+
<body>
12+
<header>
13+
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
14+
<div class="container-fluid">
15+
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Excel_Formulas</a>
16+
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
17+
aria-expanded="false" aria-label="Toggle navigation">
18+
<span class="navbar-toggler-icon"></span>
19+
</button>
20+
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
21+
<ul class="navbar-nav flex-grow-1">
22+
<li class="nav-item">
23+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
24+
</li>
25+
<li class="nav-item">
26+
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
27+
</li>
28+
</ul>
29+
</div>
30+
</div>
31+
</nav>
32+
</header>
33+
<div class="container">
34+
<main role="main" class="pb-3">
35+
@RenderBody()
36+
</main>
37+
</div>
38+
39+
<footer class="border-top footer text-muted">
40+
<div class="container">
41+
&copy; 2025 - Excel_Formulas - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
42+
</div>
43+
</footer>
44+
<script src="~/lib/jquery/dist/jquery.min.js"></script>
45+
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
46+
<script src="~/js/site.js" asp-append-version="true"></script>
47+
@await RenderSectionAsync("Scripts", required: false)
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)