C#操作Word实用实例浅析

开发 后端
C#操作Word实用实例主要向你介绍了一个实现功能:在一个指定的Word文档中查找指定的关键字,并打印出包含该关键字的段落的C#操作Word实用实例。

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

  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有所帮助。

【编辑推荐】

  1. C#操作xml文件实例详解
  2. C#操作Word书签实例浅析
  3. C#操作Word表的实例浅析
  4. C#操作Word表格的常见操作
  5. C#操作Word表格的彪悍实例
责任编辑:仲衡 来源: ieee.org.cn
相关推荐

2009-08-19 09:42:52

C#操作Word书签

2009-08-19 11:34:06

C#操作Word

2009-08-19 11:28:41

C#操作Word

2009-08-19 10:25:14

C#操作Word

2009-08-28 17:34:14

读取word文档

2009-09-01 13:13:28

C#打开Word文档

2009-08-18 13:49:21

C# 操作Excel

2009-08-18 16:04:12

C# 操作Excel

2009-08-31 18:38:59

C#写文件

2009-08-20 11:07:07

C#共享内存

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-19 16:30:55

C#操作Access数

2009-08-19 14:12:23

C#操作注册表

2009-08-18 17:42:12

C#操作符重载

2009-08-18 16:49:05

C# 操作XML

2009-08-17 17:49:20

C# 枚举

2009-09-09 13:57:28

C# XML解析

2009-08-17 13:34:02

C#异步操作
点赞
收藏

51CTO技术栈公众号