日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

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

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

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

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

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

 
 
 
  1.  ///﹤summary﹥  
  2. /// 淺析C#Word文檔替換操作,在word 中查找一個字符串直接替換所需要的文本  
  3. /// ﹤/summary﹥  
  4. /// ﹤param name="strOldText"﹥原文本﹤/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 - 替換找到的所有項。  
  16.   * wdReplaceNone - 不替換找到的任何項。  
  17. * wdReplaceOne - 替換找到的第一項。  
  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是一個word文檔的Document對象.

此外還可以運用Word Application 對象Selection的Find實現(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è)置選項,然后設(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是一個Word Application 對象

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

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


分享名稱:淺析C#Word文檔替換操作
網(wǎng)站地址:http://www.dlmjj.cn/article/cosgecd.html