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

Free Support Forum - aspose.com

Adding Text to new added row

I have a question ralated to aspose words hoping you can help me..

I am trying to add rows to existing word table which I am able to do using the code below

Aspose.Words.Tables.Row newRow = (Aspose.Words.Tables.Row)lastTable.LastRow.Clone(true);

foreach (Aspose.Words.Tables.Cell cell in newRow.Cells)

{

HOW CAN I ADD TEXT TO EACH CELL LIKE BELOW:

cell1 text = "cell 1 tex";

cell2 text = "cell 3 tex";

cell3 text = "cell 3 tex";

}

The code has three cells. I am trying to add text to each cell.. Please advise.

Hi,


Thanks for your inquiry. you can try using the following code:

Document doc = new Document(@“c:\temp\in.docx”);

Table tab = doc.FirstSection.Body.Tables[0];
Row newRow = (Row)tab.LastRow.Clone(true);

int i = 1;
foreach (Cell c in newRow)
{
c.FirstParagraph.AppendChild(new Run(doc, “Cell
"
+ i + " text”));
i++;
}

tab.AppendChild(newRow);

doc.Save(@“c:\temp\out.docx”);

I hope, this will help.

Best Regards,

This works for me to some extent. this code allows me to enter the same data for each cell.. what about accessing each cell to add different text for each cell?

Thanks

Hi,


Thanks for your inquiry. First, I would like to share a few facts about a Cell node below:

  • Cell can only be a child of a Row.
  • Cell can contain block-level nodes Paragraph and Table.
  • A minimal valid cell needs to have at least one Paragraph.

Secondly, you can get a reference to any Cell from inside the Table Row and append nodes by using the following code snippet:

NodeCollection tabCollection =
doc.GetChildNodes(NodeType.Table, true);

Table tab = tabCollection[0] as Table;

Cell c = tab.Rows[2].Cells[2];

Paragraph p = c.FirstParagraph;

Run r = new Run(doc);

r.Font.Bold = true;

r.Text = "My custom text";

p.AppendChild(r);


If we can help you with anything else, please feel free to ask.

Best Regards,