Generating Dynamic table from predefine table in word template

Hi Aspose Team,
I have attatched 2 files one is Template.doc and another is Output.doc. My requirnment is as follows.

  1. I have a predefine word template with some tables. I want to add some existing rows of particular table and append to last of that table with 2 blank rows. 1 row for value and 2 is for making space between the current table and below table. you can see the both Template and Output.doc as an example what I exactly need.
  2. Once all the tables are generated, how can I set the values in column for these table through code. I don’t want to use the merge field concept here. eg:- if you see the attatchment, all headings have background color which will treat as headers for table. I want to set the values below to heading rows through code.

I will be very thankful to you If you can show me the code for my above requinment.
Thanks
Deepak

Hi Deepak,

Thanks for your inquiry.

*DeepakSharma:

  1. I have a predefine word template with some tables. I want to add some existing rows of particular table and append to last of that table with 2 blank rows. 1 row for value and 2 is for making space between the current table and below table. you can see the both Template and Output.doc as an example what I exactly need.*

Please use the Row.Clone method method to create a duplicate of the Row and insert it at the end of Table using Table.AppendChild method. Please check the following code example for your kind reference.

*DeepakSharma:

  1. Once all the tables are generated, how can I set the values in column for these table through code. I don’t want to use the merge field concept here. eg:- if you see the attatchment, all headings have background color which will treat as headers for table. I want to set the values below to heading rows through code.*

You can insert/modify text inside table’s cell. Please check the following highlighted code snippet.

Hope this helps you. Please let us know if you have any more queries.

var doc = new Document(MyDir + "Template.doc");
// Get the first table in the document
Table table = doc.FirstSection.Body.Tables[0];
// Clone the row at index 3
Row clonedRow = (Row)table.Rows[3].Clone(true);
// Append the row at the end of table
table.AppendChild(clonedRow);
// insert empty row at the end of table
Row lastrow = (Row)table.LastRow.Clone(true);
table.AppendChild(lastrow);
foreach (Cell cell in lastrow.Cells)
{
    cell.RemoveAllChildren();
    cell.EnsureMinimum();
    cell.CellFormat.ClearFormatting();
    // Set the value in empty cell
    cell.FirstParagraph.Runs.Add(new Run(doc, "New Row"));
}
doc.Save(MyDir + "Out.doc");