Articles in this section
Category / Section

How to find and replace a single row with multiple rows in a Word document

6 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can find and replace a single row with multiple rows in a Word document using C#.

Follow the steps below:

  • Get the table by its title, which you want to modify.
  • Fill the cells of the second row with placeholder values from a predefined list.
  • Create new rows by cloning the second row and populating them with dynamic cell data.
  • Insert the new rows after the second row.

Steps to find and replace a single row with multiple rows in a Word document:

  1. Create a new .NET Core console application project. Create console application in Visual Studio
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIO NuGet package reference to the project

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

  1. Include the following namespaces in Program.cs file
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO; 
  1. Use the following code example to find and replace a single row with multiple rows in a Word document.
    C#
using (FileStream inputFileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite))
{
   // Open the input Word document
   using (WordDocument document = new WordDocument(inputFileStream, FormatType.Docx))
   {
       // Find a table by the Title property.
       WTable table = document.FindItemByProperty(EntityType.Table, "Title", "DataTable") as WTable;
       // Check if the table was found.
       if (table != null)
       {
           // Get the second row of the table.
           WTableRow secondRow = table.Rows[1];
           // Insert data into the cells of the second row.
           InsertDataToCells(secondRow);
           // Add dynamic rows starting at index 2, based on the second row.
           AddDynamicRows(table, 2, secondRow);
       }
       using (FileStream outputFileStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
       {
           // Save the modified document to the output file stream.
           document.Save(outputFileStream, FormatType.Docx);
       }
   }
}
  1. Use the following helper method to insert data into the cells of a specified table row.
    C#
/// <summary>
/// Insert data into the cells of a specified table row.
/// </summary>
void InsertDataToCells(WTableRow row)
{
   // List of placeholder data to insert into the cells.
   List<string> data = new List<string> { "<<Data1>>", "<<Data2>>", "<<Data3>>", "<<Data4>>" };
   int count = 0;
   // Iterate through each cell in the specified row.
   foreach (WTableCell cell in row.Cells)
   {
       // Assign data to the particular cell.
       cell.Paragraphs[0].Text = data[count];
       count++;
   }
}
  1. Use the following helper method to add dynamic rows to a specified table at a certain index.
    C#
/// <summary>
/// Add dynamic rows to a specified table at a certain index.
/// </summary>
void AddDynamicRows(WTable table, int index, WTableRow row)
{
   // Create a list of dynamic row details.
   IEnumerable<dynamic> rowsDetails = CreateDynamicRows();
   // Iterate through each dynamic row detail.
   foreach (dynamic rowDetails in rowsDetails)
   {
       // Retrieve cell content for the new row.
       List<string> cellDetails = GetListOfCellValue(rowDetails);
       // Clone the second row to create a new row.
       WTableRow newRow = row.Clone();
       // Iterate through the cells of the cloned row.
       for (int i = 0; i < newRow.Cells.Count; i++)
       {
           // Get the cell at specific from the cloned row.
           WTableCell wTableCell = newRow.Cells[i];
           // Modify the paragraph text of the cell with the corresponding cell detail.
           wTableCell.Paragraphs[0].Text = cellDetails[i];
       }
       // Insert the newly created row at the specified index.
       table.Rows.Insert(index, newRow);
       // Increment the index for the next dynamic row.
       index++;
   }
}
  1. Use the following helper method to create dynamic rows with sample cell data.
    C#
/// <summary>
/// Create dynamic rows with sample cell data.
/// </summary>
IEnumerable<dynamic> CreateDynamicRows()
{
   // Create a list of dynamic row details.
   List<dynamic> rowDetails = new List<dynamic>();
   // Add dynamic cells to the row details list.
   rowDetails.Add(CreateDynamicCells("<<Data5>>", "<<Data6>>", "<<Data7>>", "<<Data8>>"));
   rowDetails.Add(CreateDynamicCells("<<Data9>>", "<<Data10>>", "<<Data11>>", "<<Data12>>"));
   rowDetails.Add(CreateDynamicCells("<<Data13>>", "<<Data14>>", "<<Data15>>", "<<Data16>>"));
   rowDetails.Add(CreateDynamicCells("<<Data17>>", "<<Data18>>", "<<Data19>>", "<<Data20>>"));
   // Return the list of dynamic row details.
   return rowDetails; 
}
  1. Use the following helper method to create dynamic cell data.
    C#
/// <summary>
/// Create dynamic cell data.
/// </summary>
dynamic CreateDynamicCells(string cell1, string cell2, string cell3, string cell4)
{
   // Create a new ExpandoObject for dynamic properties.
   dynamic dynamicOrder = new ExpandoObject();
   // Assign values to the dynamic object properties.
   dynamicOrder.Cell1 = cell1;
   dynamicOrder.Cell2 = cell2;
   dynamicOrder.Cell3 = cell3;
   dynamicOrder.Cell4 = cell4;
   // Return the dynamic object.
   return dynamicOrder;
}
  1. Use the following helper method to get the list of cell values.
    C#
/// <summary>
/// Get the list of cell values.
/// </summary>
List<string> GetListOfCellValue(dynamic rowDetails)
{
   List<string> cellDetails = new List<string>();
   // Add each dynamic cell value to the list.
   cellDetails.Add(rowDetails.Cell1);
   cellDetails.Add(rowDetails.Cell2);
   cellDetails.Add(rowDetails.Cell3);
   cellDetails.Add(rowDetails.Cell4);
   // Return the list of cell details.
   return cellDetails;
}

You can download a complete working sample to find and replace a single row with multiple rows in a Word document from the GitHub.

By executing the program, you will get the Word document as follows.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Conclusion

I hope you enjoyed learning about how to find and replace a single row with multiple rows in a Word document.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied