简体   繁体   中英

MailMerge #foreach list of strings using Aspose.Words

I'm currently making a MailMerge file and want to show a list of strings. In the past I've used a list of objects (for example Customer ) and was able to have something like this in the Word-doc:

{{ #foreach Customers }}
{{Name}}
{{Address}}
{{ /foreach Customers }}

Now however, I have a list of strings instead of Objects, and I simply want to show them:

{{ #foreach List }}
{{???}}
{{ /foreach List }}

So, what is supposed to go at the ??? . Or should I change the #foreach List to something like a foreach in .NET C#, ie {{ #foreach value in List }} or something similar?

I haven't been able to find a lot about MailMerge in general to be honest, and nothing about foreach in the MS Word-doc.

If this isn't possible I guess I'll have to put the string in a container-class? Like:

public class StringContainer
{
    public string String { get; set; }
}

and

{{ #foreach List }}
{{String}}
{{ /foreach List}}

EDIT:

We use Aspose.Words ( .MailMerge & .MailMerging ) for this conversion from our Data-object to data in the MS Word doc. Here is the code for the conversion:

private static byte[] GenerateDocument(Stream template, DocumentDataSource dataSource, SaveOptions saveOptions, IFieldMergingCallback fieldMergingCallback = null)
{
    var doc = new Document(template);

    doc.MailMerge.FieldMergingCallback = fieldMergingCallback;
    doc.MailMerge.UseNonMergeFields = true;
    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveContainingFields |
                                   MailMergeCleanupOptions.RemoveUnusedFields |
                                   MailMergeCleanupOptions.RemoveUnusedRegions |
                                   MailMergeCleanupOptions.RemoveEmptyParagraphs;

    doc.MailMerge.Execute(dataSource);
    doc.MailMerge.ExecuteWithRegions((IMailMergeDataSourceRoot)dataSource);

    doc.UpdateFields();

    using (var ms = new MemoryStream())
    {
        doc.Save(ms, saveOptions);
        return ms.ToArray();
    }
}

And here an example of how we use it:

public byte[] CreateLetter(string filePath, string fileName, OurDataObject data)
{
    var path = Path.Combine(filePath, fileName);

    using (var fs = File.OpenRead(path))
    {
        var dataSource = new DocumentDataSource(data);
        return GenerateDocument(fs, dataSource, new OoxmlSaveOptions(SaveFormat.Docx));
    }
}

I suggest you please use Aspose.Words LINQ Reporting Engine to achieve your requirements.

A typical template for LINQ Reporting Engine is composed of common document contents and tags that describe the template's structure and data bindings. You can form these tags using just running text that can occupy multiple paragraphs to be more descriptive.

A tag body must meet the following requirements:

  • A tag body must be surrounded by “<<” and “>>” character sequences.
  • A tag body must contain only text nodes.
  • A tag body must not be located inside markup document nodes such as StructuredDocumentTag, CustomXmlMarkup, or SmartTag.

A tag body typically consists of the following elements:

  • A tag name
  • An expression surrounded by brackets
  • A set of switches available for the tag, each of which is preceded by the “-“ character

<< tag_name [expression] –switch1 –switch2 ... >>

Particular tags can have additional elements. Some tags require closing counterparts. A closing tag has the “/” character that precedes its name. This tag's name must match to the name of the corresponding opening tag.

<< /tag_name >>

Note – Tag body elements are case-sensitive.

Please read this link and check following code example. Hope this helps you.

DocumentBuilder builder = new DocumentBuilder();
builder.Write("The items are: <<foreach [item in items]>><<[item]>>, <</foreach>>and others.");
Document doc = builder.Document;

ReportingEngine engine = new ReportingEngine();
engine.BuildReport(doc, new string[] { "Item1", "Item2", "Item3" }, "items");

doc.Save(MyDir + "out.docx");

I work with Aspose as Developer evangelist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM