VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#使用Aspose实现Word域套打

前言

在项目开发过程中我们经常会使用word书签替换功能导出一些模板类的文档,可能很多人都不知道word还有一个域替换的功能。可以实现和书签同样的效果,对比word书签,word域还可以用于一些对于格式要求严格的文档导出(将域建立在模板图片上),本文使用c#基于Aspose.Word实现word域套打功能

创建域

以word2013为例,插入-文本-文档部件-域,选择MergeField

下图示例就是插入域完毕的文档

使用Aspose.Word

注册Aspose.Words 去水印

1
2
3
4
5
6
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);
    }

域替换

1
2
3
4
5
6
7
8
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://files.cnblogs.com/files/yanpeng19940119/%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B.rar

本文链接:
https://www.cnblogs.com/yanpeng19940119/p/13909969.html


相关教程