Formatting a Table Cell with HTML in a Cloned Row

I am using a Word Doc Template which contains a Table with Bookmarks. Populating the bookmarks is not the issue. At the end of the table I am inserting a varying number of rows retrieved from the database. My approach is to CLONE the last row, clear the existing data for each cell, populate each cell, append row, and repeat the process for the next database row.

The issue I am trying to solve is the data put into the cell will contain HTML tags and I am trying to format the table cell to display as HTML. My code looks like this:

//------------------------------//
//------- Fields 09 to 12 ------//
//------------------------------//

        Aspose.Words.Tables.Table FPVTable = (Aspose.Words.Tables.Table)ESHDoc.GetChild(NodeType.Table, 0, true);

        cmd = DBUtility.CreateCommand();
        cmd.CommandText = "Usp_GetReportData";

        cmd.Parameters.Add(new SqlParameter("@Site_Id", ddlSite.SelectedItem.Value));
        cmd.Parameters.Add(new SqlParameter("@YearVal", ddlPVYear.SelectedItem.Value));
        cmd.Parameters.Add(new SqlParameter("@FA_Id", ddlFacilityArea.SelectedItem.Value));

        dt = DBUtility.ExecuteCommand(cmd);

        if (dt.Rows.Count > 0)
        {
            int CellCnt = 0;
            foreach (DataRow dr in dt.Rows)
            {
                CellCnt = 0;
                Aspose.Words.Tables.Row clonedRow = (Aspose.Words.Tables.Row)FPVTable.LastRow.Clone(true);
                foreach (Aspose.Words.Tables.Cell myCell in clonedRow.Cells)
                {
                    myCell.RemoveAllChildren();
                    myCell.EnsureMinimum();
                    if (CellCnt == 0)
                    {
                        //--- Information contains HTML tags
                        myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr["Column9"].ToString().Replace("&lt;","<").Replace("&gt;", ">")));
                    }
                    if (CellCnt == 1)
                    {
                        myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr["FSD"].ToString()));
                        myCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                    }
                    if (CellCnt == 2)
                    {
                        myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr["RAC"].ToString()));
                        myCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                    }
                    if (CellCnt == 3)
                    { 
                        //--- Information contains HTML tags ("&lt;" & "&gt;")
                        myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr["CAR"].ToString())); 
                    }


                    myCell.FirstParagraph.Runs[0].Font.Size = 8;
                    myCell.FirstParagraph.Runs[0].Font.Name = "Calibri";
                    CellCnt++;
                }
                FPVTable.AppendChild(clonedRow);
            }
        }

What formatting call am I missing or should I be taking a different approach?

After some searching I have modified my code to this:

if (dt.Rows.Count > 0)
{
int CellCnt = 0;
foreach (DataRow dr in dt.Rows)
{
CellCnt = 0;
Aspose.Words.Tables.Row clonedRow = (Aspose.Words.Tables.Row)FPVTable.LastRow.Clone(true);
foreach (Aspose.Words.Tables.Cell myCell in clonedRow.Cells)
{
myCell.RemoveAllChildren();
myCell.EnsureMinimum();
if (CellCnt == 0)
{
//— Information contains HTML tags
builder.MoveTo(myCell.FirstParagraph);
builder.InsertHtml(dr[“Column9”].ToString().Replace("<", “<”).Replace(">", “>”).Replace(" “, " “));
}
if (CellCnt == 1)
{
myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr[“FSD”].ToString()));
myCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (CellCnt == 2)
{
myCell.FirstParagraph.Runs.Add(new Run(ESHDoc, dr[“RAC”].ToString()));
myCell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
}
if (CellCnt == 3)
{
//— Information contains HTML tags
builder.MoveTo(myCell.FirstParagraph);
builder.InsertHtml(dr[“CAR”].ToString().Replace(”<”, “<”).Replace(">", “>”).Replace(" “,” "));
}

                    NodeCollection runs = myCell.GetChildNodes(NodeType.Run, true);
                    // Loop through all RUNs and add Formatting
                    foreach (Run run in runs)
                    {
                        run.Font.Size = 8;
                        run.Font.Name = "Calibri";
                    }
                    CellCnt++;
                }
                FPVTable.AppendChild(clonedRow);
            }
        }

This appears to be working, HTML is displayed, formatting is what I wanted. But if there is a better solution I am willing to listen.

@DaveKosc16,

Yes, instead of inserting new Run nodes inside Table Cells, you need to use DocumentBuilder.InsertHtml method to be able to insert formatted content from HTML string into Table (or at any other place) in Word document. You may also want to look at the following overload:

In case you have further inquiries or may need any help in future, please let us know.