Skip to content

Commit 066e9a7

Browse files
Merge pull request #248 from VijayadharshiniMathiyalagan/ES-887097-How-to-find-and-replace-a-single-row-with-multiple-rows-in-a-Word-document
Added sample to replace a single row with multiple rows
2 parents 4f6bb60 + 56081a9 commit 066e9a7

File tree

5 files changed

+171
-0
lines changed

5 files changed

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