Replace text in Word table

Good day.

Please see attached.
What I want to do is to export a string value to replace a particular cell in word table.

Will that be possible?

thank you,

Attachment: word_ee1fa4e5.7z

1 Reply 1 reply marked as answer

MR Manikandan Ravichandran Syncfusion Team September 11, 2020 06:38 PM UTC

Hi Mark,

Thank you for contacting Syncfusion support.

From the given details, we have found that your requirement is to replace the text in the table cell with another text. You can achieve our requirement in two ways. There are,

1. Replacing the text using Replace API as like below. 
document.Replace("<1>", "One", true, true);
document.Replace(
"<2>", "Two", true, true);
document.Replace(
"<3>", "Three", true, true);
document.Replace(
"<4>", "Four", true, true);
document.Replace(
"<5>", "Five", true, true); 

Please refer the below link to know more about Replace text.
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace

2. Iterating the table element and replace the text in paragraph of the table cell as like below. 
WTable table = document.LastSection.Tables[0] as WTable;

foreach
(WTableRow row in table.Rows)
{
     
foreach (WTableCell cell in row.Cells)
     {
       
foreach (TextBodyItem bodyItem in cell.ChildEntities)
        {
          
if (bodyItem is WParagraph)
           {
               
if ((bodyItem as WParagraph).Text == "<1>")
                    (bodyItem
as WParagraph).Text = "One";
               
else if ((bodyItem as WParagraph).Text == "<2>")
                    (bodyItem
as WParagraph).Text = "Two";
               
else if((bodyItem as WParagraph).Text == "<3>")
                     (bodyItem
as WParagraph).Text = "Three";
               
else if((bodyItem as WParagraph).Text == "<4>")
                     (bodyItem
as WParagraph).Text = "Four";
               
else if((bodyItem as WParagraph).Text == "<5>")
                     (bodyItem
as WParagraph).Text = "Five";
              }
         }
     }
}
 

Please refer the below link to know more about iterate table body.
https://help.syncfusion.com/file-formats/docio/working-with-tables#iterating-through-table-elements

Please let us know if you have any other questions.

Regards,
Manikandan Ravichandran 


Marked as answer
Loader.
Up arrow icon