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

本文向您介绍如何使用 C# 识别 Word 中的合并字段名称,欢迎查阅!

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

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

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

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

使用 C# 识别 Word 中的合并字段名称

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

using Spire.Doc;using System;namespace MailMerge{class Program{static void Main(string[] args){//Creates Document instanceDocument document = new Document();//Loads the word documentdocument.LoadFromFile("MergeFields.docx");//Gets the collection of group namesstring[] GroupNames = document.MailMerge.GetMergeGroupNames();//Gets the collection of merge field names in a specific groupstring[] MergeFieldNamesWithinRegion = document.MailMerge.GetMergeFieldNames("Products");// Gets the collection of all the merge field namesstring[] 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]);}}}}

截屏:

Identify Merge Field Names in Word with C#

欢迎下载|体验更多E-iceblue产品

如需获取更多产品相关信息请咨询在线客服  


标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年3月19日
下一篇 2022年3月19日

相关推荐

发表回复

登录后才能评论