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

Free Support Forum - aspose.com

Aspose Columnar table

Hi Aspose team,

I’m working with Aspose.Words and I’d like to present a certain table in a column format, rather than a row format.

As per https://docs.aspose.com/display/wordssharepoint/Merge+Regions

The merge fields are populated with data from the first row of the data source, then the whole region is repeated, and the new fields are populated with data from the second row, and so on. Follow these simple rules when marking a region:
• TableStart and TableEnd fields must be inside the same section in the document.
• If used inside a table, TableStart and TableEnd must be inside the same row in the table.
• You should always have a pair of matching TableStart and TableEnd fields with the same table name.

I’d like to know if there is any workaround to allow mergefield rows to be presented as columns on the document.

Please advise

@saranshkejriwal

Thanks for your inquiry. Aspose.Words’ mail merge engine does not merge the data column wise. Could you please share your input and expected output Word documents here for our reference? We will then provide you more information about your query.

Thanks for your prompt response Tahir.manzoor.

Here is my requirement:

Given a datatable as follows:

image.png (1.0 KB)

I’d like to insert this into a word document as follows:
image.png (619 Bytes)

What should be the layout of my mergefields to achieve this?

Please advise

@saranshkejriwal

Thanks for your inquiry. In your case, we suggest you please convert the table rows into columns after performing the mail merge operation. Following code example shows how to change rows into columns. Hope this helps you.

We have attached the input and output documents with this post for your kind reference. Docs.zip (18.5 KB)

Document doc = new Document(MyDir + @"in.docx");
Table mergetable = doc.FirstSection.Body.Tables[0];

Table table = new Table(doc);

for (int i = 0; i < mergetable.FirstRow.Count; i++)
{
    Row row = new Row(doc);
    table.AppendChild(row);

    Column column = Column.FromIndex(mergetable, i);
    foreach (Cell cell in column.Cells)
    {
        row.Cells.Add(cell.Clone(true));
    }
}

table.PreferredWidth = PreferredWidth.FromPercent(30);
Paragraph node  = (Paragraph)mergetable.ParentNode.InsertAfter(new Paragraph(doc), mergetable);
node.ParentNode.InsertAfter(table, node);

mergetable.Remove();

doc.Save(MyDir + "19.2.docx");

Please get the code of Column class from following article.
Working with Columns