C#在word文档中替换字符串
来源:互联网 发布:ifashion淘宝衣服怎样 编辑:程序博客网 时间:2024/05/16 11:34
在文档中搜索和替换字符串,先在word文档中标记字符串,然后再搜索标记字符串并用新的字符串替换标记字符串.主要是先选择整个文档,然后使用Find的Execute方法查找指定字符串并替换为相应字符串.
以下实现方式之一,使用文档(Document )对象的 Content 属性选择整个文档。
此外还可以 运用Word Application 对象Selection的Find.
以下实现方式之一,使用文档(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 ;
}
- C#在word文档中替换字符串
- C#在word文档中替换字符串
- C# 如何在Word文档中添加,替换和删除书签
- C# 在Word文档中生成条形码
- C# 在Word文档中生成条形码
- C# 在word中查找及替换文本
- C# 替换Word文档中的图片
- 用C#实现在Word文档中搜索文本
- 用C#实现在Word文档中搜索文本
- 用C#实现在Word文档中搜索文本
- 用C#实现在Word文档中搜索文本
- 用C#实现在Word文档中搜索文本
- C#实现在Word文档中搜索文本
- 用C#实现在Word文档中搜索文本
- 如何用C#在Word文档中搜索文本
- 编程技巧:如何在C#中读取Word文档
- C#在word文档中连续插入表格
- C# 如何在Word文档中插入艺术字
- 在outlook2003中预览附件图片
- informix常用命令的用法说明
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图
- 喜欢是干脆的
- linux下设置固定IP
- C#在word文档中替换字符串
- 最绝的隐藏文件方法
- 共享打印机提示无法保存打印机设置,操作无法完成
- 小站推广
- 域名及域名投资
- 在MyEclipse中配置JNDI DataSource
- Javascript错误代码解释大全
- 百年极限论是自相矛盾的理论的原因
- 常用HTML代码解释