C#操作Word实用实例浅析

C#操作Word实用实例:下面是我自己写的一段完整的代码,功能:在一个指定的Word文档中查找指定的关键字,并打印出包含该关键字的段落。使用的Range对象。现在让我们看看具体的实现过程:

成都创新互联公司 - 电信机房托管,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,电信机房托管,西南服务器托管,四川/成都大带宽,机柜大带宽,四川老牌IDC服务商

 
 
 
  1. using System;  
  2. using System.Collections;  
  3. using Word;  
  4.  //C#操作Word实用实例
  5. namespace SearchWordDoc  
  6. {  
  7.     /// summary﹥  
  8.     /// SearchWordDo﹤c's summary  
  9.     /// ﹤/summary﹥  
  10.     public class SearchWordDoc  
  11.     {  
  12.    // search word in document.  
  13.    // strName is the document name which is searched.  
  14.    // strFind is the key word or phrase.  
  15.    // return the match paragraphs.  
  16.    public ArrayList swd(string strFName,   
  17. string strFind)  
  18.    {  
  19.   ArrayList textsFound = new ArrayList();    
  20.  
  21. // matched texts  
  22.   object missingValue = Type.Missing;  
  23.   Word.ApplicationClass wdApp = null;   
  24. // Word Application object  
  25.    //C#操作Word实用实例
  26.   try 
  27.   {  
  28.  object fName = strFName as object;  
  29.  wdApp = new ApplicationClass();   
  30. // create a Word application object  
  31.  Word.Document wdDoc = wdApp.Documents.Open(  
  32. ref fName, ref missingValue,  
  33. ref missingValue, ref missingValue,  
  34.  ref missingValue,  
  35. ref missingValue, ref missingValue,  
  36.  ref missingValue,  
  37. ref missingValue, ref missingValue,  
  38.  ref missingValue,  
  39. ref missingValue, ref missingValue,  
  40.  ref missingValue,  
  41. ref missingValue, ref missingValue);   
  42. // open a Word object  
  43.  
  44.  // the Word object has paragraphs or not  
  45.  if (wdDoc.Paragraphs != null && wdDoc.Paragraphs.Count ﹥ 0)  
  46.  {  
  47. int count = wdDoc.Paragraphs.Count;   
  48.  // the number of doc paragraphs  
  49. Word.Range rng;  // Word.Range object  
  50. Word.Find fnd;   // Word.Find object  
  51.  
  52. Console.WriteLine("There are {0}   
  53. paragraphs in document '{1}'.", count, strFName);  
  54.  //C#操作Word实用实例
  55. for (int i = 1; i ﹤= count; ++ i)     
  56.  // search key words in every paragraphs  
  57. {  
  58.     rng = wdDoc.Paragraphs[i].Range;  
  59.     fnd = rng.Find;  
  60.     fnd.ClearFormatting();  
  61.     fnd.Text = strFind;  
  62.  
  63.     if (fnd.Execute(ref missingValue,   
  64. ref missingValue,  
  65.    ref missingValue, ref missingValue,   
  66. ref missingValue,  
  67.    ref missingValue, ref missingValue,   
  68. ref missingValue,  
  69.    ref missingValue, ref missingValue,   
  70. ref missingValue,  
  71.    ref missingValue, ref missingValue,   
  72. ref missingValue,  
  73.    ref missingValue))  
  74.     {  
  75.    // if find the matched paragrahps,   
  76. add it into the textsFound ArrayList.  
  77.    textsFound.Add("[" + i.ToString() + "]   
  78. " + wdDoc.Paragraphs[i].Range.Text.Trim());  
  79.     }  
  80. }  
  81.  }  //C#操作Word实用实例
  82.   }  
  83.   catch (NullReferenceException e)  
  84.   {  
  85.  Console.WriteLine(e.ToString());  
  86.  wdApp.Quit(ref missingValue,  
  87.  ref missingValue, ref missingValue);  
  88.   }  
  89.   catch (Exception e)  
  90.   {  
  91.  Console.WriteLine(e.ToString());  
  92.  wdApp.Quit(ref missingValue,  
  93.  ref missingValue, ref missingValue);  
  94.   }  
  95.  
  96.   // release the Word application object  
  97.   wdApp.Quit(ref missingValue,  
  98.  ref missingValue, ref missingValue);  
  99.  
  100.   return textsFound;  
  101.    }  
  102.  
  103.    // Display usage  
  104.    public void usage()  
  105.    {  
  106.   Console.WriteLine("\nUsage:  
  107.  SearchWordDoc doc_name string_found " +  
  108.  "[start_paragraph_NO.]\n\t\t[end_paragraph_NO.]");  
  109.  
  110.    }  //C#操作Word实用实例
  111.  
  112.    // Print the result  
  113.    public void printText(ArrayList lst)  
  114.    {  
  115.   if (lst == null)  
  116.   {  
  117.  Console.WriteLine("Error: Null ArrayList.\n");  
  118.  return;  
  119.   }  
  120.  
  121.   int len = lst.Count;  
  122.   for (int i = 0; i ﹤ len; ++ i)  
  123.   {  
  124.  Console.WriteLine("\t" + lst[i] as string);  
  125.   }  
  126.  
  127.   Console.WriteLine("\nThere are {0} records.", len);  
  128.    }  
  129.  
  130.    // Function Main  
  131.    public static void Main(string[] args)  
  132.    {  
  133.   ArrayList textsFound = new ArrayList();  
  134.   SearchWordDoc sobject = new SearchWordDoc();  
  135.  //C#操作Word实用实例
  136.   switch (args.Length)  
  137.   {  
  138.  case 0:  
  139.  case 1:  
  140. sobject.usage();  
  141. break;  
  142.  case 2:  
  143. textsFound = sobject.swd(args[0], args[1]);  
  144. Console.WriteLine("Search Result:\n");  
  145. sobject.printText(textsFound);  
  146. break;  
  147.  default:  
  148. sobject.usage();  
  149. break;  
  150.   }  
  151.    }  
  152.     }  
  153. } // End 

C#对Word的操作和对Excel等的操作方法很相似。

C#操作Word实用实例的基本情况就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。

网站标题:C#操作Word实用实例浅析
文章位置:http://www.shufengxianlan.com/qtweb/news28/158278.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联