Skip to content

Commit 0e5a92c

Browse files
authored
Merge pull request #295 from SyncfusionExamples/1008384-picture
1008384 - How to Work with Pictures in Excel Documents Using the .NET Excel Library
2 parents b1dd382 + 4d0e27d commit 0e5a92c

78 files changed

Lines changed: 74915 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Pictures.Models;
3+
using Syncfusion.XlsIO;
4+
using System.Diagnostics;
5+
6+
namespace Pictures.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 AddPicture()
18+
{
19+
// Create a new Excel engine instance
20+
ExcelEngine excelEngine = new ExcelEngine();
21+
22+
// Access the Excel application
23+
IApplication application = excelEngine.Excel;
24+
25+
// Set default Excel version to XLSX
26+
application.DefaultVersion = ExcelVersion.Xlsx;
27+
28+
// Open the input workbook stream
29+
FileStream fileStream = new FileStream("Data\\Input.xlsx", FileMode.Open, FileAccess.Read);
30+
31+
// Open the workbook from the file stream
32+
IWorkbook workbook = application.Workbooks.Open(fileStream);
33+
34+
// Get the first worksheet
35+
IWorksheet worksheet = workbook.Worksheets[0];
36+
37+
// Open the image stream to be inserted
38+
FileStream imageStream = new FileStream("Data\\Image.png", FileMode.Open, FileAccess.Read);
39+
40+
// Add the image at row 10, column 2
41+
IPictureShape picture = worksheet.Pictures.AddPicture(10, 2, imageStream);
42+
43+
// Set the picture width
44+
picture.Width = 320;
45+
46+
// Set the picture height
47+
picture.Height = 180;
48+
49+
//Return the workbook as a downloadable file
50+
return ExportWorkbook(workbook, "AddPicture.xlsx");
51+
}
52+
53+
public IActionResult ModifyPicture()
54+
{
55+
// Create a new Excel engine instance
56+
ExcelEngine excelEngine = new ExcelEngine();
57+
58+
// Access the Excel application
59+
IApplication application = excelEngine.Excel;
60+
61+
// Set default Excel version to XLSX
62+
application.DefaultVersion = ExcelVersion.Xlsx;
63+
64+
// Open the input workbook stream
65+
FileStream fileStream = new FileStream("Data\\Template.xlsx", FileMode.Open, FileAccess.Read);
66+
67+
// Open the workbook from the stream
68+
IWorkbook workbook = application.Workbooks.Open(fileStream);
69+
70+
// Get the first worksheet
71+
IWorksheet worksheet = workbook.Worksheets[0];
72+
73+
// Get the first picture in the worksh
74+
IPictureShape picture = worksheet.Pictures[0];
75+
76+
// Set vertical position
77+
picture.Top = 150;
78+
79+
// Set horizontal position
80+
picture.Left = 200;
81+
82+
// Set the new width of the picture
83+
picture.Width = 420;
84+
85+
// Set the new height of the picture
86+
picture.Height = 240;
87+
88+
//Return the workbook as a downloadable file
89+
return ExportWorkbook(workbook, "ModifyPicture.xlsx");
90+
}
91+
92+
public IActionResult RemovePicture()
93+
{
94+
// Create a new Excel engine instance
95+
ExcelEngine excelEngine = new ExcelEngine();
96+
97+
// Access the Excel application
98+
IApplication application = excelEngine.Excel;
99+
100+
// Set default Excel version to XLSX
101+
application.DefaultVersion = ExcelVersion.Xlsx;
102+
103+
// Open the template workbook stream
104+
FileStream fileStream = new FileStream("Data\\Template.xlsx", FileMode.Open, FileAccess.Read);
105+
106+
// Open the workbook from the stream
107+
IWorkbook workbook = application.Workbooks.Open(fileStream);
108+
109+
// Get the first worksheet
110+
IWorksheet worksheet = workbook.Worksheets[0];
111+
112+
// Get the first picture in the worksheet
113+
IPictureShape picture = worksheet.Pictures[0];
114+
115+
// Remove the selected picture from the worksheet
116+
picture.Remove();
117+
118+
119+
// return the updated workbook as a downloadable file
120+
return ExportWorkbook(workbook, "RemovePicture.xlsx");
121+
}
122+
123+
private FileStreamResult ExportWorkbook(IWorkbook workbook, string fileName)
124+
{
125+
// Create an in-memory stream to hold the workbook
126+
MemoryStream stream = new MemoryStream();
127+
128+
// Save the workbook into the memory stream
129+
workbook.SaveAs(stream);
130+
131+
// Reset the stream position to the beginnin
132+
stream.Position = 0;
133+
134+
// Return the stream as a downloadable file
135+
return File(stream, "application/xlsx", fileName);
136+
}
137+
138+
public IActionResult Index()
139+
{
140+
return View();
141+
}
142+
143+
public IActionResult Privacy()
144+
{
145+
return View();
146+
}
147+
148+
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
149+
public IActionResult Error()
150+
{
151+
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
152+
}
153+
}
154+
}

‎Videos/Pictures/Data/Image.png‎

61.8 KB
Loading

‎Videos/Pictures/Data/Input.xlsx‎

10.6 KB
Binary file not shown.

‎Videos/Pictures/Data/Template.xlsx‎

65.7 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 Pictures.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}

‎Videos/Pictures/Pictures.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="*" />
11+
</ItemGroup>
12+
13+
</Project>

‎Videos/Pictures/Program.cs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace Pictures
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:8336",
8+
"sslPort": 44356
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5018",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7087;http://localhost:5018",
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@{
2+
ViewData["Title"] = "Home Page";
3+
}
4+
5+
<div>
6+
<h2 style="margin-bottom: 20px">Working with Pictures in Excel</h2>
7+
<div>
8+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size:18px;"
9+
onclick="location.href='@Url.Action("AddPicture", "Home")'">
10+
Add Picture
11+
</button>
12+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size: 18px;"
13+
onclick="location.href='@Url.Action("ModifyPicture", "Home")'">
14+
Modify Picture
15+
</button>
16+
<button style="width: 200px; margin-bottom: 20px; height: 40px;display:block;font-size: 18px;"
17+
onclick="location.href='@Url.Action("RemovePicture", "Home")'">
18+
Remove Picture
19+
</button>
20+
</div>
21+
</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>

0 commit comments

Comments
 (0)