...
Note that using nested repeat blocks is only supported by using docx files.
Code
Code Block |
---|
public void GenerateDocument() { WordTemplate //Select the state for which we will print out the data this.stateCode = "MA"; /* Create WordTemplate */ WordTemplate wt = new WordTemplate(); wt.Open( /* Open the template document */ string templatePath = @"..\..\WordTemplateFiles\NestedMailMergeTemplateMailMergeTemplate.docx"; wt.Open(templatePath); //Retrieve Get the Datadatatable fromfor theany source,state a by parsing that state's specific csv file with a helper method DataTable dtRegionData = GetMailMergeDataGetCustomerData(@"..\..\WordData\NestedMailMergeData.csv"MailMergeDemoData"+this.stateCode+".CSV"); // set dt as the datasource for the repeatblock and "MailMerge" as /* Set the bookmark. DataTable as //the thissource willfor cause wordwriter to bind each letter on a different page of the output file. wt.SetRepeatBlock(dt, "MailMerge"); the mailmerge.*/ wt.SetMailMerge(RegionData); wt.Process(); // /* Save the Documentdocument*/ in the desired location string docName = String.Format("MailMerge-{0}", this.stateCode); wt.Save(@"..\..\WordOutputFiles\NestedMailMerge"+ docName+ "_output"+".docx"); } //Helper/ method<summary> to parse the csv files /// Parses the data from the csv file with the data for a specific state /// Note: only the MA csvfile exists because this is for sample purposes /// </summary> /// <returns>DataTable of mailing data</returns> private DataTable GetMailMergeDataGetCustomerData(stringString csvFileName) { DataTable dt = new DataTable() dt; using (GenericParserAdapter parser = new GenericParserAdapter(csvFileName)) { parser.ColumnDelimiter = ','; parser.FirstRowHasHeader = true; dt = parser.GetDataTable(); } return dt; } |
Downloads
Panel |
---|
Template: NestedMailMergeTemplate.docx Data: NestedMailMergeData.csv Output: NestedMailMerge_output.docx |