Free Support Forum - aspose.com

Repeating table

I need to have each row of data formatted as a table, i.e. with some columns shown as rows.

When I put the TableStart/TableEnd merge fields outside the table, I end up with several tables separated by 2 line feeds, where I actually want just one table with each record formatted into 2 rows.

I've tried putting the template table within a table, but get same result.

How can I get rid of the line feeds and have it all as one repeating table?

Hope I've explained it OK,

Graeme

I've just created an image to explain it - see attached.

Any takers? I'm totally new to Aspose and I'm sure this must be simple but I haven't figured it out yet.

TIA

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think that you can try using MergeField event handler in this case. Please see the following code and attached documents:

ArrayList listOfRows = new ArrayList();

public void Test253()

{

//Create some datasource

DataTable myTable = new DataTable("myTable");

myTable.Columns.Add("field1");

myTable.Columns.Add("field2");

myTable.Columns.Add("field3");

for (int i = 0; i < 10; i++)

{

myTable.Rows.Add(new object[] { i.ToString(), "Some data" + i.ToString(), "Some text" + i.ToString() });

}

//perform mailmerge

Document doc = new Document(@"Test253\in.doc");

doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField_253);

doc.MailMerge.ExecuteWithRegions(myTable);

//create result table

Table mainTable = (listOfRows[0] as Row).ParentTable;

for (int i = 0; i < listOfRows.Count; i++)

{

mainTable.AppendChild((Row)listOfRows[i]);

}

doc.Save(@"Test253\out.doc");

}

void MailMerge_MergeField_253(object sender, MergeFieldEventArgs e)

{

if (e.FieldName == "field1")

{

Table parentTable = (e.Field.Start.GetAncestor(NodeType.Table) as Table);

//Get rows in table

RowCollection rows = parentTable.Rows;

foreach (Row row in rows)

{

listOfRows.Add(row);

}

//Remove empty paragraphs before and after table

parentTable.NextSibling.Remove();

parentTable.PreviousSibling.Remove();

}

}

Hope this helps.

Best regards.

I just used the two lines to remove the paragraphs before and after the tables from your event handler and that does the trick. Not sure why you store all the rows then re-create the table...

Thanks

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

I do this because if you just remove empty paragraphs between rows there will be several tables in the document. But if you store all rows in the intermediate array and then append to the main table there will be only one table.

If you don’t need this behavior then you can just remove empty paragraphs.

Best regards.