Skip to content

Commit a840ca5

Browse files
ES-887097- Added sample
1 parent 74702a8 commit a840ca5

File tree

5 files changed

+175
-0
lines changed

5 files changed

+175
-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 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-row-with-multiple-rows", "Replace-row-with-multiple-rows\Replace-row-with-multiple-rows.csproj", "{7D07A56C-8B23-4CFE-9E8C-903C5C1EF6CB}"
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+
{7D07A56C-8B23-4CFE-9E8C-903C5C1EF6CB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{7D07A56C-8B23-4CFE-9E8C-903C5C1EF6CB}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{7D07A56C-8B23-4CFE-9E8C-903C5C1EF6CB}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{7D07A56C-8B23-4CFE-9E8C-903C5C1EF6CB}.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 = {F9812314-7D24-42E7-8090-5B2969EB47E9}
24+
EndGlobalSection
25+
EndGlobal
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Dynamic;
4+
5+
using (FileStream inputFileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
6+
{
7+
// Open the input Word document
8+
using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
9+
{
10+
// Find a table with its alternate text (Title property).
11+
WTable table = document.FindItemByProperty(EntityType.Table, "Title", "DataTable") as WTable;
12+
// Check if the table was found.
13+
if (table != null)
14+
{
15+
// Get the second row of the table.
16+
WTableRow secondRow = table.Rows[1];
17+
// Insert data into the cells of the second row.
18+
InsertDataToCells(secondRow);
19+
// Add dynamic rows starting at index 2, based on the second row.
20+
AddDyamicRows(table, 2, secondRow);
21+
}
22+
23+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
24+
{
25+
// Save the modified document to the output file stream.
26+
document.Save(outputFileStream, FormatType.Docx);
27+
}
28+
}
29+
}
30+
31+
/// <summary>
32+
/// Insert data into the cells of a specified table row.
33+
/// </summary>
34+
void InsertDataToCells(WTableRow row)
35+
{
36+
// List of placeholder data to insert into the cells.
37+
List<string> data = new List<string> { "<<Data1>>", "<<Data2>>", "<<Data3>>", "<<Data4>>" };
38+
int count = 0;
39+
// Iterate through each cell in the specified row.
40+
foreach (WTableCell cell in row.Cells)
41+
{
42+
// Assign data to the particular cell.
43+
cell.Paragraphs[0].Text = data[count];
44+
count++;
45+
}
46+
}
47+
48+
/// <summary>
49+
/// Add dynamic rows to a specified table at a certain index.
50+
/// </summary>
51+
void AddDyamicRows(WTable table, int index, WTableRow row)
52+
{
53+
// Create a list of dynamic row details.
54+
IEnumerable<dynamic> rowsDetails = CreateDyamicRows();
55+
// Iterate through each dynamic row detail.
56+
foreach (dynamic rowDetails in rowsDetails)
57+
{
58+
// Retrieve cell content for the new row.
59+
List<string> cellDetails = GetListOfCellValue(rowDetails);
60+
// Clone the second row to create a new row.
61+
WTableRow newRow = row.Clone();
62+
// Iterate through the cells of the cloned row.
63+
for (int i = 0; i < newRow.Cells.Count; i++)
64+
{
65+
// Get the cell at specific from the cloned row.
66+
WTableCell wTableCell = newRow.Cells[i];
67+
// Modify the paragraph text of the cell with the corresponding cell detail.
68+
wTableCell.Paragraphs[0].Text = cellDetails[i];
69+
}
70+
// Insert the newly created row at the specified index.
71+
table.Rows.Insert(index, newRow);
72+
// Increment the index for the next dynamic row.
73+
index++;
74+
}
75+
}
76+
77+
/// <summary>
78+
/// Create dynamic rows with sample cell data.
79+
/// </summary>
80+
IEnumerable<dynamic> CreateDyamicRows()
81+
{
82+
// Create a list of dynamic row details.
83+
List<dynamic> rowDetails = new List<dynamic>();
84+
85+
// Add dynamic cells to the row details list.
86+
rowDetails.Add(CreateDynamicCells("<<Data5>>", "<<Data6>>", "<<Data7>>", "<<Data8>>"));
87+
rowDetails.Add(CreateDynamicCells("<<Data9>>", "<<Data10>>", "<<Data11>>", "<<Data12>>"));
88+
rowDetails.Add(CreateDynamicCells("<<Data13>>", "<<Data14>>", "<<Data15>>", "<<Data16>>"));
89+
rowDetails.Add(CreateDynamicCells("<<Data17>>", "<<Data18>>", "<<Data19>>", "<<Data20>>"));
90+
// Return the list of dynamic row details.
91+
return rowDetails;
92+
}
93+
94+
/// <summary>
95+
/// Create dynamic cell data.
96+
/// </summary>
97+
dynamic CreateDynamicCells(string cell1, string cell2, string cell3, string cell4)
98+
{
99+
// Create a new ExpandoObject for dynamic properties.
100+
dynamic dynamicOrder = new ExpandoObject();
101+
102+
// Assign values to the dynamic object properties.
103+
dynamicOrder.Cell1 = cell1;
104+
dynamicOrder.Cell2 = cell2;
105+
dynamicOrder.Cell3 = cell3;
106+
dynamicOrder.Cell4 = cell4;
107+
// Return the dynamic object.
108+
return dynamicOrder;
109+
}
110+
111+
/// <summary>
112+
/// Convert the dynamic values to a list of strings.
113+
/// </summary>
114+
List<string> GetListOfCellValue(dynamic rowDetails)
115+
{
116+
List<string> cellDetails = new List<string>();
117+
118+
// Add each dynamic cell value to the list.
119+
cellDetails.Add(rowDetails.Cell1);
120+
cellDetails.Add(rowDetails.Cell2);
121+
cellDetails.Add(rowDetails.Cell3);
122+
cellDetails.Add(rowDetails.Cell4);
123+
// Return the list of cell details.
124+
return cellDetails;
125+
}
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>Replace_row_with_multiple_rows</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>

0 commit comments

Comments
 (0)