Hi Team,
I am using mail merge template (MS Word) with Mustache Syntax feature and I want to achieve column merging in mail merge template (MS Word) with Mustache Syntax but not able to find any suitable solution for it. Please help me on same.
I have attached below files:
My template (Docx file) - Having mail merge template
My Output without Colmn merge (png file) - Generated file/report with above docx template
Expected Output with column merge (png file) - In First column need column merging (It is based on the first column i.e. “Fabric Deck” grouping)
Expected Output with column merge.PNG (90.7 KB)
My Output without Colmn merge.PNG (63.9 KB)
My Template.docx (26.2 KB)
Thanks & Regards,
Gaurav
@gauravgarg047 Unfortunately, Mail Merge does not provide such feature. However, this feature is available in LINQ Reporting Engine. See how to dynamically merge cells upon building the report.
If you are limited to use Mail Merge, you can try using IFieldMergingCallback. For example see the following code and simple template: in.docx (12.6 KB) out.docx (10.0 KB)
DataTable dt = new DataTable("Members");
dt.Columns.Add("Name");
dt.Columns.Add("Method");
dt.Rows.Add("Document", "Save");
dt.Rows.Add("Document", "UpdateFields");
dt.Rows.Add("Document", "UpdatePageLayout");
dt.Rows.Add("DocumentBuilder", "MoveTo");
dt.Rows.Add("DocumentBuilder", "MoveToBookmark");
dt.Rows.Add("DocumentBuilder", "InsertHtml");
dt.Rows.Add("DocumentBuilder", "InsertDocument");
Document doc = new Document(@"C:\Temp\in.docx");
doc.MailMerge.FieldMergingCallback = new GroupValuesInColumn();
doc.MailMerge.ExecuteWithRegions(dt);
doc.Save(@"C:\Temp\out.docx");
private class GroupValuesInColumn : IFieldMergingCallback
{
public void FieldMerging(FieldMergingArgs args)
{
// Use Name field for grouping.
if (args.FieldName == "Name")
{
// Get the current cell and cell in the previouse row with the same index.
Cell currentCell = (Cell)args.Field.Start.GetAncestor(NodeType.Cell);
// Compare current value with current group value.
string currentText = args.FieldValue.ToString();
if (mCurrentGroupValue == currentText)
{
currentCell.CellFormat.VerticalMerge = CellMerge.Previous;
}
else
{
currentCell.CellFormat.VerticalMerge = CellMerge.First;
mCurrentGroupValue = currentText;
}
}
}
public void ImageFieldMerging(ImageFieldMergingArgs args)
{
// do nothing here.
}
private string mCurrentGroupValue = "";
}
I have used regular merge fields in the template, but the same approach can be used with Mustache Syntax too.