Word控件Spire.Doc 【邮件合并】教程(六): 使用 C# 识别 Word 中的合并字段名称

我们经常遇到这样的场景,我们需要将数据合并到其他人创建的合并字段中,并且我们不确定合并字段的名称。所以为了完成邮件合并的目的,首先我们需要读取所有合并字段的名称。

Spire.Doc for.NET 最新下载

Spire.Doc.Reporting 命名空间中的 MailMerge 类公开了以下方法,这些方法返回 Word 文档中的合并字段名称或组(区域)名称的集合。

  • public string[] GetMergeFieldNames():返回所有合并字段名称的集合。
  • public string[] GetMergeFieldNames(string groupName):返回特定组内合并字段名称的集合。
  • public string[] GetMergeGroupNames():返回组名的集合。

为了更好地演示,我们使用以下示例文档:


下面的例子详细说明了如何读取上述word文档中的组名和合并字段。

using Spire.Doc;
using System;
namespace MailMerge
{
class Program
{
static void Main(string[] args)
{

//Creates Document instance
Document document = new Document();

//Loads the word document
document.LoadFromFile("MergeFields.docx");

//Gets the collection of group names
string[] GroupNames = document.MailMerge.GetMergeGroupNames();

//Gets the collection of merge field names in a specific group
string[] MergeFieldNamesWithinRegion = document.MailMerge.GetMergeFieldNames("Products");

// Gets the collection of all the merge field names
string[] MergeFieldNames = document.MailMerge.GetMergeFieldNames();

Console.WriteLine("----------------Group Names-----------------------------------------");
for (int i = 0; i < GroupNames.Length; i++)
{
Console.WriteLine(GroupNames[i]);
}

Console.WriteLine("----------------Merge field names within a specific group-----------");
for (int j = 0; j < MergeFieldNamesWithinRegion.Length; j++)
{
Console.WriteLine(MergeFieldNamesWithinRegion[j]);
}

Console.WriteLine("----------------All of the merge field names------------------------");
for (int k = 0; k < MergeFieldNames.Length; k++)
{
Console.WriteLine(MergeFieldNames[k]);
}

}

}
}

截屏: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值