Skip to content

Commit 3cceaee

Browse files
Merge pull request #287 from VijayadharshiniMathiyalagan/ES-889573-How-to-add-rows-with-dynamic-data-into-an-existing-table-in-Word-document
Add sample for add rows with dynamic data into an existing table
2 parents 3c7919b + dbefc98 commit 3cceaee

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35417.141 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-rows-with-dynamic-data", "Add-rows-with-dynamic-data\Add-rows-with-dynamic-data.csproj", "{416B2F2F-28AF-42DD-B2D8-43E539F5C009}"
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+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{416B2F2F-28AF-42DD-B2D8-43E539F5C009}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_rows_with_dynamic_data</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
4+
using (FileStream inputStream = new FileStream(@"Data/Template.docx", FileMode.Open, FileAccess.Read))
5+
{
6+
// Open an existing Word document.
7+
using (WordDocument document = new WordDocument(inputStream, FormatType.Automatic))
8+
{
9+
// Find the table with title.
10+
WTable table = document.FindItemByProperty(EntityType.Table, "Title", "Template") as WTable;
11+
// Check if the table with the specified title was found.
12+
if (table != null)
13+
{
14+
// Define new data to add to the table.
15+
List<string[]> rowData =
16+
[
17+
["3.", "Banana", "20"],
18+
["4.", "Grapes", "70"]
19+
];
20+
for (int j = 0; j < rowData.Count; j++)
21+
{
22+
string[] data = rowData[j];
23+
// Add a new row to the table.
24+
WTableRow row = table.AddRow(false);
25+
for (int i = 0; i < data.Length; i++)
26+
{
27+
string cellData = data[i];
28+
// Get the cell at the current index.
29+
WTableCell cell = row.Cells[i];
30+
// Add a paragraph to the cell.
31+
WParagraph cellParagraph = cell.AddParagraph() as WParagraph;
32+
// Add the cell data as text within the cell's paragraph.
33+
cellParagraph.AppendText(cellData);
34+
}
35+
}
36+
}
37+
using (FileStream outputStream = new FileStream(@"Output/Result.docx", FileMode.Create, FileAccess.Write))
38+
{
39+
// Save the modified Word document.
40+
document.Save(outputStream, FormatType.Docx);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)