简单 Mail Merge 运作
一个简单的 Mail Merge 用于填充 Mail Merge 模板内的字段,包含来自数据源的所需数据(单表表示)。 所以它类似于经典 Mail Merge 在 Microsoft Word.
您可以在模板中添加一个或多个合并字段,然后执行简单的 Mail Merge 操作。 如果您的模板不包含任何合并区域,建议使用它。
使用此类型的主要限制是整个文档内容将针对数据源中的每条记录重复。
如何执行一个简单的 Mail Merge 运作
一旦你的模板准备好,你就可以开始执行简单的 Mail Merge 操作。 Aspose.Words 允许你执行一个简单的 Mail Merge 使用不同的操作 Execute 接受各种数据对象作为数据源的方法。
下面的代码示例演示如何执行一个简单的 Mail Merge 使用其中一个操作 Execute 方法:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->InsertField(u" MERGEFIELD CustomerName "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Item "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Quantity "); | |
auto boxHelper = [](const char16_t* value) { return System::ObjectExt::Box<String>(value); }; | |
// Fill the fields in the document with user data. | |
doc->get_MailMerge()->Execute( | |
MakeArray<String>({u"CustomerName", u"Item", u"Quantity"}), | |
MakeArray<SharedPtr<System::Object>>( | |
{ boxHelper(u"John Doe"), boxHelper(u"Hawaiian"), boxHelper(u"2")})); | |
doc->Save(ArtifactsDir + u"BaseOperations.SimpleMailMerge.docx"); |
您可以在执行simple之前注意到文档之间的差异 mail merge:

并执行简单后 mail merge:

如何创建多个合并文档
在 Aspose.Words,标准 Mail Merge 操作仅使用数据源中的内容填充单个文档。 所以,你需要执行 Mail Merge 多次操作以创建几个合并的文档作为输出。
下面的代码示例演示如何在一个 Mail Merge 操作与 SQLiteCpp:
// For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-C.git. | |
void MultipleDocumentsMailMerge() | |
{ | |
auto doc = MakeObject<Document>(); | |
auto builder = MakeObject<DocumentBuilder>(doc); | |
builder->InsertField(u" MERGEFIELD FullName "); | |
builder->InsertParagraph(); | |
builder->InsertField(u" MERGEFIELD Address "); | |
// Fill the fields in the document with user data. | |
SQLite::Database database{ (std::string)(DatabaseDir + u"customers.db3") }; | |
SQLite::Statement query{ database, "SELECT * FROM Customers" }; | |
auto dataSource = MakeObject<CustomersRowMailMergeDataSource>(query); | |
int32_t counter = 1; | |
while (query.executeStep()) | |
{ | |
dataSource->Reset(); | |
doc->get_MailMerge()->Execute(dataSource); | |
doc->Save(ArtifactsDir + u"BaseOperations.MultipleDocumentsMailMerge_" + counter++ + u".docx"); | |
} | |
} | |
class CustomersRowMailMergeDataSource : public MailMerging::IMailMergeDataSource | |
{ | |
public: | |
CustomersRowMailMergeDataSource(SQLite::Statement& query) : mQuery(query) {} | |
String get_TableName() override | |
{ | |
return u"Customers"; | |
} | |
bool GetValue(String fieldName, SharedPtr<Object>& fieldValue) override | |
{ | |
auto boxHelper = [](const std::string& value) { return System::ObjectExt::Box<String>(String::FromUtf8(value)); }; | |
if (fieldName == u"FullName") | |
{ | |
fieldValue = boxHelper(mQuery.getColumn(1).getString()); | |
return true; | |
} | |
if (fieldName == u"Address") | |
{ | |
fieldValue = boxHelper(mQuery.getColumn(2).getString()); | |
return true; | |
} | |
fieldValue.reset(); | |
return false; | |
} | |
bool MoveNext() override | |
{ | |
if (!isInitialized) | |
{ | |
isInitialized = true; | |
return true; | |
} | |
return false; | |
} | |
SharedPtr<IMailMergeDataSource> GetChildDataSource(String tableName) override | |
{ | |
return nullptr; | |
} | |
void Reset() | |
{ | |
isInitialized = false; | |
} | |
private: | |
SQLite::Statement& mQuery; | |
bool isInitialized = false; | |
}; |