...
Note that using nested repeat blocks is only supported by using docx files.
Code
Code Block |
---|
public void GenerateDocument() { //Select the state for which we will print out the data this.stateCode = "MA"; // Create WordTemplate WordTemplate wt = new WordTemplate(); // Open the template document string templatePath = wt.Open(@"..\..\WordTemplateFiles\MailMergeTemplateNestedMailMergeTemplate.docx"; wt.Open(templatePath); //Retrieve Get the datatableData forfrom anythe statesource, bya parsingcsv thatfile state'swith specifica csvhelper filemethod DataTable RegionDatadt = GetCustomerDataGetMailMergeData(@"..\..\WordData\MailMergeDemoData"+this.stateCode+".CSV"NestedMailMergeData.csv"); // Setset thedt DataTable as the sourcedatasource for the mailmerge.repeatblock and "MailMerge" as wt.SetMailMerge(RegionData);the bookmark. wt.Process(); // Save the document // this will cause wordwriter to bind each letter on a different page of the output file. string docName = String.Format("MailMerge-{0}", this.stateCodewt.SetRepeatBlock(dt, "MailMerge"); wt.Save(@"..\..\WordOutputFiles\"+ docName+ "_output"+".docx"Process(); } /// <summary> /// Parses the data fromSave the Document in the csvdesired filelocation with the data for a specific state wt.Save(@"..\..\WordOutputFiles\NestedMailMerge_output.docx"); /// Note: only the MA csvfile exists} because this is for sample purposes ///Helper </summary>method to parse the csv files with data /// <returns>DataTable of mailing data</returns> private DataTable GetCustomerDataGetMailMergeData(Stringstring csvFileName) { DataTable dt = new DataTable(); 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 |