您的位置:首页 > 编程语言 > ASP

.net 使用 Aspose.Words 进行 Word替换操作

2013-04-02 22:46 711 查看

.net 使用 Aspose.Words 进行 Word替换操作

背景:

  之前在工作中,需要实现Word打印功能,并且插入图片。当时采取的方式则是使用书签进行操作。首先在word内插入书签,完成后,存为模板。程序加载该模板,找到书签,并在指定位置写入文字即可。

  后期维护过程中,发现模板经常需要变更,但是书签在word中不方便查看,用户在编辑word的时候容易出错。于是想采取特殊字符串标识的方式进行替换。此时,图片的插入就存在问题,光标无法直接移动到指定字符串。

资源下载:

  源代码

开发思路:

  查阅 Aspose.Words提供的API,发现有Range类有该方法:

public int Replace(Regex pattern, IReplacingCallback handler, bool isForward);


  该方法则是在使用正则表达式进行文档内替换的同时可以执行IReplacingCallback接口。

  具体实现代码如下:



/* ==============================================================================
* 文 件 名:Program
* 功能描述:
* Copyright (c) 2013 武汉经纬视通科技有限公司
* 创 建 人: alone
* 创建时间: 2013/4/2 11:16:19
* 修 改 人:
* 修改时间:
* 修改描述:
* 版    本: v1.0.0.0
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using Aspose.Words;
namespace WordDemo
{
class Program
{
static void Main(string[] args)
{
var dic = new Dictionary<string, string>();
dic.Add("姓名", "杨幂");
dic.Add("学历", "本科");
dic.Add("联系方式", "02759597666");
dic.Add("邮箱", "304885433@qq.com");
dic.Add("头像", ".//1.jpg");
//使用书签操作
Document doc = new Document(".//1.doc");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (var key in dic.Keys)
{
builder.MoveToBookmark(key);
if (key != "头像")
{
builder.Write(dic[key]);
}
else
{
builder.InsertImage(dic[key]);
}
}
doc.Save("书签操作.doc");//也可以保存为1.doc 兼容03-07
Console.WriteLine("已经完成书签操作");
//使用特殊字符串替换
doc = new Document(".//2.doc");
foreach (var key in dic.Keys)
{
if (key != "头像")
{
var repStr = string.Format("&{0}&", key);
doc.Range.Replace(repStr, dic[key], false, false);
}
else
{
Regex reg = new Regex("&头像&");
doc.Range.Replace(reg, new ReplaceAndInsertImage(".//1.jpg"), false);
}
}
doc.Save("字符串替换操作.doc");//也可以保存为1.doc 兼容03-07
Console.WriteLine("已经完成特殊字符串替换操作");
Console.ReadKey();
}
}

public class ReplaceAndInsertImage : IReplacingCallback
{
/// <summary>
/// 需要插入的图片路径
/// </summary>
public string url { get; set; }

public ReplaceAndInsertImage(string url)
{
this.url = url;
}

public ReplaceAction Replacing(ReplacingArgs e)
{
//获取当前节点
var node = e.MatchNode;
//获取当前文档
Document doc = node.Document as Document;
DocumentBuilder builder = new DocumentBuilder(doc);
//将光标移动到指定节点
builder.MoveTo(node);
//插入图片
builder.InsertImage(url);
return ReplaceAction.Replace;
}
}

}




模板如图:

  


生成文档如图:

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: