Skip to content

Commit e3561cc

Browse files
Merge pull request #447 from SyncfusionExamples/ES-263347-Convert-excel-to-image-and-insert-word
ES-263347- Add the sample Convert-excel-to-image-and-insert-word
2 parents 4a924b7 + 1c536c7 commit e3561cc

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-excel-to-image-and-insert-word", "Convert-excel-to-image-and-insert-word\Convert-excel-to-image-and-insert-word.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
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>net8.0</TargetFramework>
6+
<RootNamespace>Convert_excel_to_image_and_insert_word</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
<PackageReference Include="Syncfusion.XlsIORenderer.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\Template.xlsx">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\.gitkeep">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using Syncfusion.XlsIO;
2+
using Syncfusion.XlsIORenderer;
3+
using Syncfusion.DocIO;
4+
using Syncfusion.DocIO.DLS;
5+
using System.IO;
6+
7+
8+
// Initialize XlsIO renderer
9+
using (ExcelEngine excelEngine = new ExcelEngine())
10+
{
11+
IApplication application = excelEngine.Excel;
12+
13+
// Initialize the XlsIORenderer (required for ConvertToImage)
14+
application.XlsIORenderer = new XlsIORenderer();
15+
16+
// Open the Excel file
17+
IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Template.xlsx"));
18+
19+
// Create the Word document
20+
using (WordDocument document = new WordDocument())
21+
{
22+
for (int i = 0; i < workbook.Worksheets.Count; i++)
23+
{
24+
IWorksheet worksheet = workbook.Worksheets[i];
25+
26+
// Convert used range of worksheet to image
27+
using (MemoryStream imageStream = new MemoryStream())
28+
{
29+
worksheet.ConvertToImage(worksheet.UsedRange, imageStream);
30+
imageStream.Position = 0;
31+
32+
// Add a new section for each worksheet
33+
IWSection section = document.AddSection();
34+
section.PageSetup.Orientation = PageOrientation.Landscape;
35+
IWParagraph paragraph = section.AddParagraph();
36+
37+
// Insert the Excel image into the paragraph
38+
IWPicture picture = paragraph.AppendPicture(imageStream);
39+
40+
// Optionally set image dimensions
41+
picture.Width = 600;
42+
picture.Height = 380;
43+
}
44+
}
45+
46+
// Save the Word document
47+
document.Save(Path.GetFullPath(@"Output/Result.docx"), FormatType.Docx);
48+
}
49+
}

0 commit comments

Comments
 (0)