Extend an existing table


#1

Hi!
I would like to know if it is possible to extend an existing table by adding new rows?
If so, what is the way to do this?
Thanks. Christophe


#2

!!! CANCEL !!!
I’did not have updated my Aspose.Word version.
In the new version, I found all I wanted.
Christophe


#3

Great, just in the case anybody else has the same question it is easy to do:

//Select the table somehow, for example using XPath
Table table = doc.SelectNode("//Table[1]"); //Select the 1st table in the document.

//Remove the first row for example (shows how to use indexed access to Row).
table.RemoveChild(table.Rows[0]);

//Remove the last row (shows how to use FirstRow and LastRow properties)
table.RemoveChild(table.LastRow);


//Add a row using the typed Rows collection
Row row = new Row(doc);
table.Rows.Add(row);

//Insert a row at the beginning using the typed Rows collection.
table.Rows.Insert(0, new Row(doc));

//Insert a row after the first row using XmlElement-like methods
table.InsertAfter(new Row(doc), table.FirstRow).


Now the row is in the table you need to make sure the row contains one or more Cell nodes and add some Paragraph nodes with Runs of text using the same set of add and insert methods.

Cell cell = (Cell)row.AppendChild(new Cell(doc));
Paragraph para = (Paragraph)cell.AppendChild(new Paragraph(doc));
Run run = (Run)para.AppendChild(new Run(doc, “Hello, text in a Cell.”));