We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Clone & Add Rows to Tables in Word Document & Update Content of Cells using C# .NET

Hello, I currently have several tables in a document which I am trying to add rows to and then add information to the cells in the rows. I have started by using the clone method to ensure that the rows and cells are the same as the existing table but I am struggling to then update the text that is left in the cells as a result of cloning the previous row. Could you point me in the right direction to adding rows that are the same as the rest of the table and then adding text to the cells?
Many Thanks
Luke

Hi
Thanks for your inquiry. Please try using the following code:

// Open document
Document doc = new Document(@"Test297\in.doc");
// Create DocumentBuilder
DocumentBuilder builder = new DocumentBuilder(doc);
// Get table from the first section of document
Table myTable = doc.FirstSection.Body.Tables[0];
// Get last row of table
Row myRow = myTable.LastRow;
// Add 10 more rows into the table
for (int rowIndex = 0; rowIndex < 10; rowIndex++)
{
    // Clone last row
    Row newRow = (Row)myRow.Clone(true);
    // Insert created row into the table
    myTable.AppendChild(newRow);
    // Loop through all cells in row
    foreach (Cell cell in newRow)
    {
        // Remove old content from the cell
        // Remove paragraphs
        while (cell.Paragraphs.Count > 1)
        {
            cell.LastParagraph.Remove();
        }
        // Remove content of first paragraph
        cell.FirstParagraph.ChildNodes.Clear();
        // Move DocumentBuilder cursor to the cell
        builder.MoveTo(cell.FirstParagraph);
        // Insert new content
        builder.Write("This is new content");
    }
}
// Save document
doc.Save(@"Test297\out.doc");

Hope this helps.
Best regards.

Perfect. Thanks