3

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#, i.e. {{ #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));
    }
}
4

1 回答 1

0

我建议您使用Aspose.Words LINQ Reporting Engine来满足您的要求。

LINQ 报告引擎的典型模板由描述模板结构和数据绑定的通用文档内容和标签组成。您可以仅使用可以占据多个段落的运行文本来形成这些标签,以更具描述性。

标签正文必须满足以下要求:

  • 标签正文必须由“<<”和“>>”字符序列包围。
  • 标签正文必须只包含文本节点。
  • 标记正文不得位于标记文档节点内,例如 StructuredDocumentTag、CustomXmlMarkup 或 SmartTag。

标签正文通常由以下元素组成:

  • 一个标签名称
  • 用括号括起来的表达式
  • 一组可用于标签的开关,每个开关前面都有“-”字符

<< tag_name [表达式] –switch1 –switch2 ... >>

特定的标签可以有额外的元素。一些标签需要关闭对应物。结束标签的名称前面有“/”字符。此标签的名称必须与相应的开始标签的名称相匹配。

<< /tag_name >>

注 – 标记正文元素区分大小写。

请阅读链接并检查以下代码示例。希望这对您有所帮助。

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");

我与 Aspose 合作,担任开发人员传道者。

于 2015-11-02T11:11:48.077 回答