自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#操作Word實用實例淺析

開發(fā) 后端
C#操作Word實用實例主要向你介紹了一個實現(xiàn)功能:在一個指定的Word文檔中查找指定的關(guān)鍵字,并打印出包含該關(guān)鍵字的段落的C#操作Word實用實例。

C#操作Word實用實例:下面是我自己寫的一段完整的代碼,功能:在一個指定的Word文檔中查找指定的關(guān)鍵字,并打印出包含該關(guān)鍵字的段落。使用的Range對象。現(xiàn)在讓我們看看具體的實現(xiàn)過程:

  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實用實例的基本情況就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。

【編輯推薦】

  1. C#操作xml文件實例詳解
  2. C#操作Word書簽實例淺析
  3. C#操作Word表的實例淺析
  4. C#操作Word表格的常見操作
  5. C#操作Word表格的彪悍實例
責(zé)任編輯:仲衡 來源: ieee.org.cn
相關(guān)推薦

2009-08-19 11:34:06

C#操作Word

2009-08-19 09:42:52

C#操作Word書簽

2009-08-19 11:28:41

C#操作Word

2009-08-19 10:25:14

C#操作Word

2009-09-01 13:13:28

C#打開Word文檔

2009-08-28 17:34:14

讀取word文檔

2009-08-18 13:49:21

C# 操作Excel

2009-08-31 18:38:59

C#寫文件

2009-08-18 16:04:12

C# 操作Excel

2009-08-20 11:07:07

C#共享內(nèi)存

2009-08-26 13:48:31

C#打印條碼

2009-08-19 10:46:48

C#操作Word表格

2009-08-27 13:30:11

C# interfac

2009-08-18 17:42:12

C#操作符重載

2009-08-19 14:12:23

C#操作注冊表

2009-08-19 16:30:55

C#操作Access數(shù)

2009-08-17 13:34:02

C#異步操作

2009-08-27 17:59:56

C#接口定義

2009-08-19 17:44:15

C#操作文本文件

2009-08-17 17:49:20

C# 枚舉
點贊
收藏

51CTO技術(shù)棧公眾號