日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析C#Word文檔替換操作

淺析C#Word文檔替換操作的操作時(shí)在文檔中搜索和替換字符串,先在word文檔中標(biāo)記字符串,然后再搜索標(biāo)記字符串并用新的字符串替換標(biāo)記字符串.主要是先選擇整個(gè)文檔,然后使用Find的Execute方法查找指定字符串并替換為相應(yīng)字符串.

成都創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的遼寧網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

以下實(shí)現(xiàn)淺析C#Word文檔替換操作的方式:

使用文檔(Document )對(duì)象的 Content 屬性選擇整個(gè)文檔。

 
 
 
  1.  ///﹤summary﹥  
  2. /// 淺析C#Word文檔替換操作,在word 中查找一個(gè)字符串直接替換所需要的文本  
  3. /// ﹤/summary﹥  
  4. /// ﹤param name="strOldText"﹥?cè)谋京?param﹥  
  5. /// ﹤param name="strNewText"﹥新文本﹤/param﹥  
  6. /// ﹤returns﹥﹤/returns﹥  
  7. public bool Replace(string strOldText,string strNewText)  
  8. {  
  9. this.oDoc.Content.Find.Text = strOldText ;  
  10. object FindText,  ReplaceWith, Replace ;//   
  11. object MissingValue = Type.Missing;   
  12. FindText = strOldText ;//要查找的文本  
  13. ReplaceWith = strNewText ;//替換文本  
  14.    Replace = Word.WdReplace.wdReplaceAll ;  
  15. /**//*wdReplaceAll - 替換找到的所有項(xiàng)。  
  16.   * wdReplaceNone - 不替換找到的任何項(xiàng)。  
  17. * wdReplaceOne - 替換找到的第一項(xiàng)。  
  18. * */ 
  19. this.oDoc.Content.Find.ClearFormatting();  
  20. //移除Find的搜索文本和段落格式設(shè)置  
  21. if (this.oDoc.Content.Find.Execute(  
  22. ref FindText,ref MissingValue,  
  23. ref MissingValue,ref MissingValue,  
  24. ref MissingValue,ref MissingValue,  
  25. ref MissingValue,ref MissingValue,ref MissingValue,  
  26. ref ReplaceWith,ref Replace,  
  27. ref MissingValue,ref MissingValue,  
  28. ref MissingValue,ref MissingValue))  
  29. {  
  30. return true ;  
  31. }  
  32. return false ;  
  33.  
  34. }  

說明:其中oDoc是一個(gè)word文檔的Document對(duì)象.

此外還可以運(yùn)用Word Application 對(duì)象Selection的Find實(shí)現(xiàn)淺析C#Word文檔替換操作.

 
 
 
  1. public bool SearchReplace(string strOldText,string strNewText)  
  2. {   
  3. object replaceAll = Word.WdReplace.wdReplaceAll;   
  4. object missing = Type.Missing;   
  5.  
  6. //首先清除任何現(xiàn)有的格式設(shè)置選項(xiàng),然后設(shè)置搜索字符串 strOldText。  
  7. this.oWordApplic.Selection.Find.ClearFormatting();   
  8. oWordApplic.Selection.Find.Text = strOldText;   
  9.  
  10. oWordApplic.Selection.Find.Replacement.ClearFormatting();   
  11. oWordApplic.Selection.Find.Replacement.Text = strNewText;   
  12.  
  13. if (oWordApplic.Selection.Find.Execute(  
  14. ref missing, ref missing,   
  15. ref missing, ref missing, ref missing,   
  16. ref missing, ref missing,   
  17. ref missing, ref missing, ref missing,  
  18. ref replaceAll, ref missing,   
  19. ref missing, ref missing, ref missing))  
  20. {  
  21. return true ;  
  22. }  
  23. return false ;  
  24. }  

注:oWordApplic是一個(gè)Word Application 對(duì)象

當(dāng)然也可以使用word文檔的書簽BookMark.使用 Bookmark 的 Range 屬性可將文本插入占位符書簽,以便能夠在以后檢索文本,或替換已包含文本的書簽中的文本。

淺析C#Word文檔替換操作的具體內(nèi)容就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)淺析C#Word文檔替換操作有所幫助。


網(wǎng)站標(biāo)題:淺析C#Word文檔替換操作
標(biāo)題路徑:http://m.5511xx.com/article/cosgecd.html