Spire.Doc 教程:用C#识别Word中的合并字段名称

咱们常常遇到一个场景,就是须要将数据合并到由其余人建立的合并字段,咱们不能肯定合并字段的名称。 因此为了完成邮件合并的目的,首先咱们须要读取全部合并字段的名称。html

Spire.Doc.Reporting命名空间中的MailMerge类公开了如下方法,该方法返回一个word文档中的合并字段名称或组(区域)名称的集合。htm

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

了更好的演示,咱们使用如下示例文档:blog

图片1

如下示例详细说明了如何读取上述单词文档中的组名称和合并字段。图片

//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]);
}

截图:ci

图片2

慧都控件网文档

相关文章
相关标签/搜索