C#在word文档中替换字符串
在文档中搜索和替换字符串,先在word文档中标记字符串,然后再搜索标记字符串并用新的字符串替换标记字符串.主要是先选择整个文档,然后使用Find的Execute方法查找指定字符串并替换为相应字符串.
以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
此外还可以 运用Word Application 对象Selection的Find.
Find.Execute 方法详细介绍请看文档:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-us,VS.80).aspx
当然也可以使用word文档的书签BookMark.使用 Bookmark 的 Range 属性可将文本插入占位符书签,以便能够在以后检索文本,或替换已包含文本的书签中的文本。可以参考http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/dv_wrcore/html/wrtskhowtoupdatebookmarktext.asp
以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
///<summary>
/// 在word 中查找一个字符串直接替换所需要的文本
/// </summary>
/// <param name="strOldText">原文本</param>
/// <param name="strNewText">新文本</param>
/// <returns></returns>
public bool Replace(string strOldText,string strNewText)
{
this.oDoc.Content.Find.Text = strOldText ;
object FindText, ReplaceWith, Replace ;//
object MissingValue = Type.Missing;
FindText = strOldText ;//要查找的文本
ReplaceWith = strNewText ;//替换文本
Replace = Word.WdReplace.wdReplaceAll ;/*wdReplaceAll - 替换找到的所有项。
* wdReplaceNone - 不替换找到的任何项。
* wdReplaceOne - 替换找到的第一项。
* */
this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
if (this.oDoc.Content.Find.Execute(
ref FindText,ref MissingValue,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref ReplaceWith,ref Replace,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue))
{
return true ;
}
return false ;
}
说明:其中oDoc是一个word文档的Document对象./// 在word 中查找一个字符串直接替换所需要的文本
/// </summary>
/// <param name="strOldText">原文本</param>
/// <param name="strNewText">新文本</param>
/// <returns></returns>
public bool Replace(string strOldText,string strNewText)
{
this.oDoc.Content.Find.Text = strOldText ;
object FindText, ReplaceWith, Replace ;//
object MissingValue = Type.Missing;
FindText = strOldText ;//要查找的文本
ReplaceWith = strNewText ;//替换文本
Replace = Word.WdReplace.wdReplaceAll ;/*wdReplaceAll - 替换找到的所有项。
* wdReplaceNone - 不替换找到的任何项。
* wdReplaceOne - 替换找到的第一项。
* */
this.oDoc.Content.Find.ClearFormatting();//移除Find的搜索文本和段落格式设置
if (this.oDoc.Content.Find.Execute(
ref FindText,ref MissingValue,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue,ref MissingValue,
ref ReplaceWith,ref Replace,
ref MissingValue,ref MissingValue,
ref MissingValue,ref MissingValue))
{
return true ;
}
return false ;
}
此外还可以 运用Word Application 对象Selection的Find.
public bool SearchReplace(string strOldText,string strNewText)
{
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
//首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
this.oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Find.Text = strOldText;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = strNewText;
if (oWordApplic.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing))
{
return true ;
}
return false ;
}
注:oWordApplic是一个Word Application 对象{
object replaceAll = Word.WdReplace.wdReplaceAll;
object missing = Type.Missing;
//首先清除任何现有的格式设置选项,然后设置搜索字符串 strOldText。
this.oWordApplic.Selection.Find.ClearFormatting();
oWordApplic.Selection.Find.Text = strOldText;
oWordApplic.Selection.Find.Replacement.ClearFormatting();
oWordApplic.Selection.Find.Replacement.Text = strNewText;
if (oWordApplic.Selection.Find.Execute(
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref replaceAll, ref missing, ref missing, ref missing, ref missing))
{
return true ;
}
return false ;
}
Find.Execute 方法详细介绍请看文档:http://msdn2.microsoft.com/zh-cn/library/microsoft.office.interop.word.find.execute(en-us,VS.80).aspx
当然也可以使用word文档的书签BookMark.使用 Bookmark 的 Range 属性可将文本插入占位符书签,以便能够在以后检索文本,或替换已包含文本的书签中的文本。可以参考http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/dv_wrcore/html/wrtskhowtoupdatebookmarktext.asp
作者:jingle lin
出处:http://jinglelin.cnblogs.com
出处:http://jinglelin.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。