C# MailMerge#使用Aspose.Words处理每个字符串列表
我目前正在制作一个邮件合并文件,并希望显示字符串列表。在过去,我使用了一个对象列表(例如C# MailMerge#使用Aspose.Words处理每个字符串列表,c#,string,ms-word,mailmerge,aspose.words,C#,String,Ms Word,Mailmerge,Aspose.words,我目前正在制作一个邮件合并文件,并希望显示字符串列表。在过去,我使用了一个对象列表(例如Customer),并且能够在Word文档中包含类似的内容: {{ #foreach Customers }} {{Name}} {{Address}} {{ /foreach Customers }} 然而,现在我有了一个字符串列表,而不是对象,我只想显示它们: {{ #foreach List }} {{???}} {{ /foreach List }} 那么,在?上应该做什么呢。或者我应该将#for
Customer
),并且能够在Word文档中包含类似的内容:
{{ #foreach Customers }}
{{Name}}
{{Address}}
{{ /foreach Customers }}
然而,现在我有了一个字符串列表
,而不是对象,我只想显示它们:
{{ #foreach List }}
{{???}}
{{ /foreach List }}
那么,在?
上应该做什么呢。或者我应该将#foreach列表
更改为类似.NETC#中的foreach,即{{{foreach value in List}}
或类似的内容吗
老实说,我还没有找到很多关于MailMerge的信息,也没有在MS Word文档中找到任何关于foreach的信息
如果这不可能,我想我必须将字符串放入容器类中?比如:
public class StringContainer
{
public string String { get; set; }
}
及
编辑: 我们使用
Aspose.Words
(.MailMerge
和.MailMerging
)将数据对象转换为MS Word文档中的数据。以下是转换代码:
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();
}
}
下面是我们如何使用它的示例:
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));
}
}
我建议您使用,以达到您的要求
LINQ Reporting Engine的典型模板由描述模板结构和数据绑定的公共文档内容和标记组成。您可以使用只占用多个段落的运行文本来形成这些标记,以便更具描述性
标签主体必须满足以下要求:
- 标记正文必须由“”字符序列包围
- 标记正文必须仅包含文本节点
- 标记正文不能位于标记文档节点内,例如 StructuredDocumentTag、CustomXmlMarkup或SmartTag
- 标记名
- 用括号括起来的表达式
- 可用于标记的一组开关,每个开关前面都有 “一”字
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");
DocumentBuilder=newdocumentbuilder();
builder.Write(“项目是:,和其他。”);
单据单据=builder.Document;
ReportingEngine engine=新的ReportingEngine();
BuildReport(doc,新字符串[]{“Item1”、“Item2”、“Item3”},“items”);
文档保存(MyDir+“out.docx”);
我与Aspose合作,担任开发人员宣传员。我以前从未见过这种语法。这是内置功能还是附加功能?@lc。这是Aspose.Words库转换数据的
MailMerge
功能。我编辑了我的问题,以展示如何使用我们的数据转换word文档的示例。
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");