Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Mon Feb 22, 2021 7:44 am

Hi,

is it possible to fill a template with nested groups/tables by only passing one class or one list?
For example: I have a template with customers and each customer has his own invoicings. Now I want to execute a MailMerge by only passing a list of the customers, because each of them has a list of their own invoicings. So I don't see the need of creating an additional Dictionary.

timo.loehr
 
Posts: 2
Joined: Tue Jan 12, 2021 3:25 pm

Mon Feb 22, 2021 10:11 am

Hello,

Thanks for your inquiry.
Attached are the sample code and my template file for your reference. If this does not meet your needs well, please provide your template file, your class and your desired output. Then we will investigate further.

Code: Select all
       
using Spire.Doc;
using Spire.Doc.Reporting;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace _24478Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<MergeModel> mlist = new List<MergeModel>
            {
                new MergeModel("Tom","171-5557788",new List<MergeModel.Comment>{new MergeModel.Comment("Comment 1",DateTime.Now),new MergeModel.Comment("Comment 2",DateTime.Now)}),
                new MergeModel("Lucy","171-5556625",new List<MergeModel.Comment>{new MergeModel.Comment("Comment 3",DateTime.Now)}),
                new MergeModel("Jerry","171-5553342",new List<MergeModel.Comment>{new MergeModel.Comment("Comment 4",DateTime.Now)})
            };

            //Wirte the list to xml file
            string XmlFileName = "List.xml";
            WirteToXml(mlist, XmlFileName);

            Document document = new Document();
            document.LoadFromFile("NestedMailMerge.docx");

            //Read the xml content into a data set
            DataSet dataSet = new DataSet();
            dataSet.ReadXml(XmlFileName);

            List<DictionaryEntry> list = new List<DictionaryEntry>();
            DictionaryEntry dictionaryEntry = new DictionaryEntry("MergeModel", string.Empty);
            list.Add(dictionaryEntry);
            dictionaryEntry = new DictionaryEntry("Comments", "MergeModel_ID = %MergeModel.MergeModel_ID%");
            list.Add(dictionaryEntry);

            document.MailMerge.ExecuteWidthNestedRegion(dataSet, list);
            document.SaveToFile("Result.docx", FileFormat.Docx);
        }

        public static void WirteToXml(List<MergeModel> list, string XmlFileName)
        {
            XmlDocument xmldoc = new XmlDocument();
            XmlDeclaration xmldeclaration = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
            xmldoc.AppendChild(xmldeclaration);
            XmlElement xmlelement = xmldoc.CreateElement("Data");
            xmldoc.AppendChild(xmlelement);

            for (int i = 0; i < list.Count; i++)
            {
                XmlElement xmlperson = xmldoc.CreateElement("MergeModel");
                XmlElement xmlName = xmldoc.CreateElement("Name");
                xmlName.InnerText = list[i].Name;
                xmlperson.AppendChild(xmlName);
                XmlElement xmlPhoneNumber = xmldoc.CreateElement("PhoneNumber");
                xmlPhoneNumber.InnerText = list[i].PhoneNumber.ToString();
                xmlperson.AppendChild(xmlPhoneNumber);
                for (int j = 0; j < list[i].Comments.Count; j++)
                {
                    XmlElement xmlComments = xmldoc.CreateElement("Comments");
                    XmlElement xmlCommentText = xmldoc.CreateElement("CommentText");
                    xmlCommentText.InnerText = list[i].Comments[j].CommentText.ToString();
                    xmlComments.AppendChild(xmlCommentText);

                    XmlElement xmlCommentDate = xmldoc.CreateElement("CommentDate");
                    xmlCommentDate.InnerText = list[i].Comments[j].CommentDate.ToString();
                    xmlComments.AppendChild(xmlCommentDate);
                    xmlperson.AppendChild(xmlComments);
                }
                xmlelement.AppendChild(xmlperson);
            }
            xmldoc.Save(XmlFileName);
        }

        public class MergeModel
        {
            public string Name { get; set; }
            public string PhoneNumber { get; set; }

            public List<Comment> Comments { get; set; }
            public MergeModel(string name, string phoneNumber, List<Comment> comments)
            {
                Name = name;
                PhoneNumber = phoneNumber;
                Comments = comments;
            }

            public class Comment
            {
                public string CommentText { get; set; }
                public DateTime CommentDate { get; set; }
                public Comment(string commentText, DateTime commentDate)
                {
                    CommentText = commentText;
                    CommentDate = commentDate;
                }
            }
        }
    }
}


Sincerely
Elena
E-iceblue support team
User avatar

Elena.Zhang
 
Posts: 279
Joined: Thu Jul 23, 2020 1:18 am

Return to Spire.Doc