.net使用Aspose.Words進(jìn)行Word替換操作的實(shí)現(xiàn)代碼
后期維護(hù)過(guò)程中,發(fā)現(xiàn)模板經(jīng)常需要變更,但是書(shū)簽在word中不方便查看,用戶(hù)在編輯word的時(shí)候容易出錯(cuò)。于是想采取特殊字符串標(biāo)識(shí)的方式進(jìn)行替換。此時(shí),圖片的插入就存在問(wèn)題,光標(biāo)無(wú)法直接移動(dòng)到指定字符串。
資源下載:
源代碼 http://m.fzitv.net/codes/84240.html
開(kāi)發(fā)思路:
查閱 Aspose.Words提供的API,發(fā)現(xiàn)有Range類(lèi)有該方法:
public int Replace(Regex pattern, IReplacingCallback handler, bool isForward);
該方法則是在使用正則表達(dá)式進(jìn)行文檔內(nèi)替換的同時(shí)可以執(zhí)行IReplacingCallback接口。
具體實(shí)現(xiàn)代碼如下:
/* ==============================================================================
* 文 件 名:Program
* 功能描述:
* Copyright (c) 2013 武漢經(jīng)緯視通科技有限公司
* 創(chuàng) 建 人: alone
* 創(chuàng)建時(shí)間: 2013/4/2 11:16:19
* 修 改 人:
* 修改時(shí)間:
* 修改描述:
* 版 本: 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("學(xué)歷", "本科");
dic.Add("聯(lián)系方式", "02759597666");
dic.Add("郵箱", "304885433@qq.com");
dic.Add("頭像", ".//1.jpg");
//使用書(shū)簽操作
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("書(shū)簽操作.doc");//也可以保存為1.doc 兼容03-07
Console.WriteLine("已經(jīng)完成書(shū)簽操作");
//使用特殊字符串替換
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("已經(jīng)完成特殊字符串替換操作");
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)
{
//獲取當(dāng)前節(jié)點(diǎn)
var node = e.MatchNode;
//獲取當(dāng)前文檔
Document doc = node.Document as Document;
DocumentBuilder builder = new DocumentBuilder(doc);
//將光標(biāo)移動(dòng)到指定節(jié)點(diǎn)
builder.MoveTo(node);
//插入圖片
builder.InsertImage(url);
return ReplaceAction.Replace;
}
}
}
模板如圖:

生成文檔如圖:
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的數(shù)據(jù)表格的方法
- asp.net下用Aspose.Words for .NET動(dòng)態(tài)生成word文檔中的圖片或水印的方法
- 解決用Aspose.Words,在word文檔中創(chuàng)建表格的實(shí)現(xiàn)方法
- 利用Aspose.Word控件實(shí)現(xiàn)Word文檔的操作
- C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
- 使用Aspose.Cells組件生成Excel文件實(shí)例
- 使用aspose.word 第三方的插件實(shí)現(xiàn)導(dǎo)出word
- C#使用Aspose.Cells控件讀取Excel
相關(guān)文章
C#根據(jù)前臺(tái)傳入實(shí)體名稱(chēng)實(shí)現(xiàn)動(dòng)態(tài)查詢(xún)數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了C#如何根據(jù)前臺(tái)傳入實(shí)體名稱(chēng)實(shí)現(xiàn)動(dòng)態(tài)查詢(xún)數(shù)據(jù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-04-04
C#在winform中實(shí)現(xiàn)數(shù)據(jù)增刪改查等功能
本篇文章主要是介紹了C#在winform中操作數(shù)據(jù)庫(kù),實(shí)現(xiàn)數(shù)據(jù)增刪改查,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11
C#畫(huà)筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法
這篇文章主要介紹了C#畫(huà)筆Pen保存和恢復(fù)圖形對(duì)象的設(shè)置方法,實(shí)例分析了畫(huà)筆的保存save及恢復(fù)屬性Restore的相關(guān)使用技巧,需要的朋友可以參考下2015-06-06
C# Form自定義光標(biāo)的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了C# Form自定義光標(biāo)的簡(jiǎn)單實(shí)現(xiàn),有需要的朋友可以參考一下2014-01-01
C#中實(shí)現(xiàn)在32位、64位系統(tǒng)下自動(dòng)切換不同的SQLite dll文件
這篇文章主要介紹了C#中實(shí)現(xiàn)在32位、64位系統(tǒng)下自動(dòng)切換不同的SQLite dll文件,本文使用C#代碼實(shí)現(xiàn)DLL文件的切換,需要的朋友可以參考下2014-09-09
Unity UGUI的Toggle復(fù)選框組件使用詳解
這篇文章主要為大家介紹了Unity UGUI的Toggle復(fù)選框組件使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

