|
| 1 | +--- |
| 2 | +title: Populate a Table with Data using Nested Mail Merge Functionality |
| 3 | +description: Learn how to populate a table with data using the Nested Mail Merge functionality. |
| 4 | +type: how-to |
| 5 | +page_title: Populate a Table with Data using Nested Mail Merge Functionality |
| 6 | +slug: populate-table-data-mail-merge |
| 7 | +tags: mail, merge, mailmerge, word, table, data, nested |
| 8 | +res_type: kb |
| 9 | +--- |
| 10 | + |
| 11 | +# Environment |
| 12 | + |
| 13 | +| Version | Product | Author | |
| 14 | +| --- | --- | ---- | |
| 15 | +| 2024.1.124 | RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 16 | + |
| 17 | +# Description |
| 18 | + |
| 19 | +Learn how to generate a Word (.DOCX) document that contains a [Table]({%slug radwordsprocessing-model-table%}) with a header row and an item row merge field. Then, passing a collection of records automatically creates and populates the data rows. |
| 20 | + |
| 21 | +# Solution |
| 22 | + |
| 23 | +To achieve the desired result, you can use the [Nested Mail Merge]({%slug radwordsprocessing-editing-mail-merge%}) functionality that [RadWordsProcessing]({%slug radwordsprocessing-overview%}) offers and populate the data rows automatically. |
| 24 | + |
| 25 | +Here's a sample code snippet that demonstrates how to achieve this: |
| 26 | + |
| 27 | +```csharp |
| 28 | +internal class Program |
| 29 | +{ |
| 30 | + static void Main(string[] args) |
| 31 | + { |
| 32 | + List<Player> players = GetPlayers(); |
| 33 | + List<PlayersHolder> playersHolders = new List<PlayersHolder>() { new PlayersHolder(players) }; |
| 34 | + RadFlowDocument document = new RadFlowDocument(); |
| 35 | + RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); |
| 36 | + |
| 37 | + Table table = editor.InsertTable(2, 2); |
| 38 | + |
| 39 | + table.Rows[0].Cells[0].Blocks.AddParagraph().Inlines.AddRun("First Name"); |
| 40 | + table.Rows[0].Cells[1].Blocks.AddParagraph().Inlines.AddRun("Last Name"); |
| 41 | + TableRow row = table.Rows[1]; |
| 42 | + var firstNameParagraph = table.Rows[1].Cells[0].Blocks.AddParagraph(); |
| 43 | + editor.MoveToParagraphStart(firstNameParagraph); |
| 44 | + editor.InsertField("MERGEFIELD TableStart:Players", ""); |
| 45 | + editor.InsertField("MERGEFIELD FirstName", ""); |
| 46 | + |
| 47 | + var lastNameParagraph = table.Rows[1].Cells[1].Blocks.AddParagraph(); |
| 48 | + editor.MoveToParagraphStart(lastNameParagraph); |
| 49 | + editor.InsertField("MERGEFIELD LastName", ""); |
| 50 | + editor.InsertField("MERGEFIELD TableEnd:Players", ""); |
| 51 | + |
| 52 | + table.PreferredWidth = new TableWidthUnit(TableWidthUnitType.Percent, 100); |
| 53 | + Border border = new Border(1, BorderStyle.Single, new ThemableColor(Colors.Black)); |
| 54 | + table.Borders = new TableBorders(border); |
| 55 | + |
| 56 | + editor.MoveToTableEnd(table); |
| 57 | + |
| 58 | + editor.InsertParagraph(); |
| 59 | + RadFlowDocument mergedDocument = document.MailMerge(playersHolders); |
| 60 | + string resultFileNameDocx = "merged.docx"; |
| 61 | + File.Delete(resultFileNameDocx); |
| 62 | + DocxFormatProvider provider = new DocxFormatProvider(); |
| 63 | + using (var output = File.OpenWrite(resultFileNameDocx)) |
| 64 | + { |
| 65 | + provider.Export(mergedDocument, output); |
| 66 | + } |
| 67 | + Process.Start(resultFileNameDocx); |
| 68 | + } |
| 69 | + |
| 70 | + public static List<Player> GetPlayers() |
| 71 | + { |
| 72 | + var Players = new List<Player>(); |
| 73 | + |
| 74 | + Players.Add(new Player() { FirstName = "John", LastName = "Baker" }); |
| 75 | + Players.Add(new Player() { FirstName = "Sam ", LastName = "Wayne" }); |
| 76 | + Players.Add(new Player() { FirstName = "Patrick", LastName = "Gibbs" }); |
| 77 | + Players.Add(new Player() { FirstName = "Oscar", LastName = "Stevens" }); |
| 78 | + Players.Add(new Player() { FirstName = "Larry", LastName = "Vodden" }); |
| 79 | + |
| 80 | + return Players; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +public class PlayersHolder |
| 85 | +{ |
| 86 | + public PlayersHolder(List<Player> players) |
| 87 | + { |
| 88 | + Players = players; |
| 89 | + } |
| 90 | + |
| 91 | + public List<Player> Players { get; set; } |
| 92 | +} |
| 93 | + |
| 94 | +public class Player { |
| 95 | + public string FirstName { get; set; } |
| 96 | + public string LastName { get; set; } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +This code snippet will produce the desired result, merging the data into the table in the Word document. |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +If you skip the MailMerge step, the following template will be produced: |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +## See Also |
| 110 | + |
| 111 | +* [Nested Mail Merge]({%slug radwordsprocessing-editing-mail-merge%}) |
| 112 | +* [Table]({%slug radwordsprocessing-model-table%}) |
0 commit comments