Skip to content

Commit a74f9a9

Browse files
committed
Added the sample for YouTube video.
1 parent a62a2ca commit a74f9a9

File tree

80 files changed

+74929
-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.

80 files changed

+74929
-0
lines changed

Videos/Comments/Comments.csproj

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.2.5" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
using System.Diagnostics;
2+
using Comments.Models;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Syncfusion.XlsIO;
5+
6+
namespace Comments.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 AddComment()
18+
{
19+
// Initialize the Excel engine
20+
ExcelEngine excelEngine = new ExcelEngine();
21+
IApplication application = excelEngine.Excel;
22+
application.DefaultVersion = ExcelVersion.Xlsx;
23+
24+
// Open the Excel workbook from the specified template
25+
IWorkbook workbook = application.Workbooks.Open("Data\\CommentsTemplate.xlsx", ExcelOpenType.Automatic);
26+
IWorksheet worksheet = workbook.Worksheets[0];
27+
28+
// Add a threaded comment to cell H16
29+
IThreadedComment threadedComment = worksheet.Range["H16"].AddThreadedComment(
30+
"What is the reason for the higher total amount of \"desk\" in the west region?",
31+
"User1",
32+
DateTime.Now
33+
);
34+
35+
// Export the modified workbook as a downloadable Excel file
36+
return ExportWorkbook(workbook, "AddComment.xlsx");
37+
}
38+
39+
public IActionResult ReplyComment()
40+
{
41+
// Initialize the Excel engine
42+
ExcelEngine excelEngine = new ExcelEngine();
43+
IApplication application = excelEngine.Excel;
44+
application.DefaultVersion = ExcelVersion.Xlsx;
45+
46+
// Open the workbook containing the comment to reply to
47+
IWorkbook workbook = application.Workbooks.Open("Data\\ReplyInput.xlsx", ExcelOpenType.Automatic);
48+
IWorksheet worksheet = workbook.Worksheets[0];
49+
50+
// Access the collection of threaded comments
51+
IThreadedComments threadedComments = worksheet.ThreadedComments;
52+
53+
// Add a reply to the first threaded comment
54+
threadedComments[0].AddReply(
55+
"The unit cost of desk is higher compared to other items in the west region. As a result, the total amount is elevated.",
56+
"User2",
57+
DateTime.Now
58+
);
59+
60+
// Export the modified workbook
61+
return ExportWorkbook(workbook, "ReplyComment.xlsx");
62+
}
63+
64+
public IActionResult ResolveComment()
65+
{
66+
// Initialize the Excel engine
67+
ExcelEngine excelEngine = new ExcelEngine();
68+
IApplication application = excelEngine.Excel;
69+
application.DefaultVersion = ExcelVersion.Xlsx;
70+
71+
// Open the workbook containing the comment to resolve
72+
IWorkbook workbook = application.Workbooks.Open("Data\\ResolveInput.xlsx", ExcelOpenType.Automatic);
73+
IWorksheet worksheet = workbook.Worksheets[0];
74+
75+
// Access the threaded comments
76+
IThreadedComments threadedComments = worksheet.ThreadedComments;
77+
78+
// Mark the first comment as resolved
79+
threadedComments[0].IsResolved = true;
80+
81+
// Export the updated workbook
82+
return ExportWorkbook(workbook, "ResolveComment.xlsx");
83+
}
84+
85+
public IActionResult DeleteComment()
86+
{
87+
// Initialize the Excel engine
88+
ExcelEngine excelEngine = new ExcelEngine();
89+
IApplication application = excelEngine.Excel;
90+
application.DefaultVersion = ExcelVersion.Xlsx;
91+
92+
// Open the workbook containing the comment to delete
93+
IWorkbook workbook = application.Workbooks.Open("Data\\DeleteInput.xlsx", ExcelOpenType.Automatic);
94+
IWorksheet worksheet = workbook.Worksheets[0];
95+
96+
// Access the threaded comments
97+
IThreadedComments threadedComments = worksheet.ThreadedComments;
98+
99+
// Delete the first threaded comment
100+
threadedComments[0].Delete();
101+
102+
// Export the updated workbook
103+
return ExportWorkbook(workbook, "DeleteComment.xlsx");
104+
}
105+
106+
public IActionResult ClearComment()
107+
{
108+
// Initialize the Excel engine
109+
ExcelEngine excelEngine = new ExcelEngine();
110+
IApplication application = excelEngine.Excel;
111+
application.DefaultVersion = ExcelVersion.Xlsx;
112+
113+
// Open the workbook containing comments to clear
114+
IWorkbook workbook = application.Workbooks.Open("Data\\ClearInput.xlsx", ExcelOpenType.Automatic);
115+
IWorksheet worksheet = workbook.Worksheets[0];
116+
117+
// Access all threaded comments
118+
IThreadedComments threadedComments = worksheet.ThreadedComments;
119+
120+
// Clear all threaded comments from the worksheet
121+
threadedComments.Clear();
122+
123+
// Export the cleaned workbook
124+
return ExportWorkbook(workbook, "ClearComment.xlsx");
125+
}
126+
127+
private FileStreamResult ExportWorkbook(IWorkbook workbook, string fileName)
128+
{
129+
130+
// Create a memory stream to hold the Excel file content
131+
MemoryStream stream = new MemoryStream();
132+
133+
// Save the workbook to the memory stream
134+
workbook.SaveAs(stream);
135+
136+
// Reset the stream position to the beginning
137+
stream.Position = 0;
138+
139+
// Return the stream as a downloadable Excel file with the specified filename
140+
return File(stream, "application/xlsx", fileName);
141+
142+
}
143+
144+
public IActionResult Index()
145+
{
146+
return View();
147+
}
148+
149+
public IActionResult Privacy()
150+
{
151+
return View();
152+
}
153+
154+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
155+
public IActionResult Error()
156+
{
157+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
158+
}
159+
}
160+
}
14.5 KB
Binary file not shown.
11.6 KB
Binary file not shown.
14.5 KB
Binary file not shown.
14.1 KB
Binary file not shown.
14.3 KB
Binary file not shown.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Comments.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

Videos/Comments/Program.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Comments
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:48728",
8+
"sslPort": 44343
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5072",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7280;http://localhost:5072",
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+
}

0 commit comments

Comments
 (0)