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

C# 替换Word文字【包含页眉、页脚、文本框、普通文字的替换】

2015-06-02 16:07 651 查看
2015-6-5 11:52备注

关于下面用micsoft处理word替换的说明,使用micsoft自带的com组件处理起来后期会有部署到服务器会出一些奇奇怪怪的问题,什么呼叫失败,标识不正确什么的

在本机测试的时候到时没什么问题,关于这些问题的解决可以看看这个

/article/8643793.html

但是建议使用word可以使用sharepoint docx.dll 去处理 所有替换就3句代码,打开,替换,保存

using Novacode;

DocX gDocument=DocX.Load(filePath);//C:aaaa.doc

gDocument.ReplaceText(strOld, strNew);

gDocument.Save();

这三句代码就可以替换下面那一大串的东东

关于Docx对象的dll可以去官网下载

http://docx.codeplex.com/

---使用

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace ToWord

{

class Program

{

static void Main(string[] args)

{

string sourceFile = @"c:\users\administrator\documents\visual studio 2012\Projects\ToWord\ToWord\WordModel.doc";

string destFile = @"c:\users\administrator\documents\visual studio 2012\Projects\ToWord\ToWord\Model\WordModel.doc";

//复制文件

System.IO.File.Copy(sourceFile, destFile, true);

WordOperate wordOperate = new WordOperate();

wordOperate.Open(destFile);

wordOperate.Replace("[项目名标签]","神没什么项目的");

wordOperate.Replace("[项目编号签]", "神没什么项目的");

wordOperate.Save();

}

}

}

using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Text;

namespace ToWord

{

public class WordOperate : IDisposable

{

private Microsoft.Office.Interop.Word._Application _app;

private Microsoft.Office.Interop.Word._Document _doc;

object _nullobj = System.Reflection.Missing.Value;

/// <summary>

/// 关闭Word进程

/// </summary>

public void KillWinword()

{

var p = Process.GetProcessesByName("WINWORD");

if (p.Any()) p[0].Kill();

}

/// <summary>

/// 打开word文档

/// </summary>

/// <param name="filePath"></param>

public void Open(string filePath)

{

_app = new Microsoft.Office.Interop.Word.ApplicationClass();

object file = filePath;

_doc = _app.Documents.Open(

ref file, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj);

}

/// <summary>

/// 替换word中的文字

/// </summary>

/// <param name="strOld">查找的文字</param>

/// <param name="strNew">替换的文字</param>

public void Replace(string strOld, string strNew)

{

//替换全局Document

_app.Selection.Find.ClearFormatting();

_app.Selection.Find.Replacement.ClearFormatting();

_app.Selection.Find.Text = strOld;

_app.Selection.Find.Replacement.Text = strNew;

object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;

_app.Selection.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref objReplace, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj);

//替换页脚的字

foreach (Microsoft.Office.Interop.Word.Section wordSection in _doc.Sections)

{

Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

footerRange.Find.ClearFormatting();

footerRange.Find.Replacement.ClearFormatting();

footerRange.Find.Text = strOld;

footerRange.Find.Replacement.Text = strNew;

footerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref objReplace, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj);

}

//替换页眉的字

//foreach (Microsoft.Office.Interop.Word.Section section in _doc.Sections)

//{

// Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;

// headerRange.Find.ClearFormatting();

// headerRange.Find.Replacement.ClearFormatting();

// headerRange.Find.Text = strOld;

// headerRange.Find.Replacement.Text = strNew;

// headerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,

// ref _nullobj, ref _nullobj, ref _nullobj,

// ref _nullobj, ref _nullobj, ref _nullobj,

// ref _nullobj, ref objReplace, ref _nullobj,

// ref _nullobj, ref _nullobj, ref _nullobj);

//}

//文本框

Microsoft.Office.Interop.Word.StoryRanges storyRanges = _doc.StoryRanges;

foreach (Microsoft.Office.Interop.Word.Range range in storyRanges)

{

Microsoft.Office.Interop.Word.Range rangeFlag = range;

if (Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory == rangeFlag.StoryType)

{

while (rangeFlag != null)

{

rangeFlag.Find.ClearFormatting();

rangeFlag.Find.Replacement.ClearFormatting();

rangeFlag.Find.Text = strOld;

rangeFlag.Find.Replacement.Text = strNew;

rangeFlag.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj,

ref _nullobj, ref objReplace, ref _nullobj,

ref _nullobj, ref _nullobj, ref _nullobj);

rangeFlag = range.NextStoryRange;

}

}

}

}

/// <summary>

/// 保存

/// </summary>

public void Save(bool disposet = true)

{

if (disposet == false)

{

_doc.Save();

}

else

{

this.Save(false);

this.Dispose();

this.KillWinword();

}

}

/// <summary>

/// 退出

/// </summary>

public void Dispose()

{

_doc.Close(ref _nullobj, ref _nullobj, ref _nullobj);

_app.Quit(ref _nullobj, ref _nullobj, ref _nullobj);

}

}

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