前言
在專案開發程序中我們經常會使用word書簽替換功能匯出一些模板類的檔案,可能很多人都不知道word還有一個域替換的功能,可以實作和書簽同樣的效果,對比word書簽,word域還可以用于一些對于格式要求嚴格的檔案匯出(將域建立在模板圖片上),本文使用c#基于Aspose.Word實作word域套打功能
創建域
以word2013為例,插入-文本-檔案部件-域,選擇MergeField

?

?
下圖示例就是插入域完畢的檔案

?
使用Aspose.Word
注冊Aspose.Words 去水印
string licenseFile = System.IO.Path.Combine(ModelPath, @"Model\Aspose.Words.lic");
if (File.Exists(licenseFile))
{
Aspose.Words.License license = new Aspose.Words.License();
license.SetLicense(licenseFile);
}
域替換
Document doc = new Document(System.IO.Path.Combine(ModelPath, TempWordName));
DocumentBuilder builder = new DocumentBuilder(doc);
if (titleField != null && valueField != null)
//檔案域的欄位賦值
doc.MailMerge.Execute(titleField, valueField);
doc.Save(SavePath + ExportWordName);
替換后效果如下

?
代碼示例
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/198446.html
標籤:.NET技术
