成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

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 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#共享內存

2009-08-26 13:48:31

C#打印條碼

2009-08-27 13:30:11

C# interfac

2009-08-19 10:46:48

C#操作Word表格

2009-08-18 17:42:12

C#操作符重載

2009-08-19 16:30:55

C#操作Access數

2009-08-19 14:12:23

C#操作注冊表

2009-08-17 13:34:02

C#異步操作

2009-08-27 17:59:56

C#接口定義

2009-08-17 17:49:20

C# 枚舉

2009-09-09 13:57:28

C# XML解析
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 亚洲精品国产电影 | 精品国产区 | 在线中文视频 | 国产精品www | 欧美jizzhd精品欧美巨大免费 | 精品久久久久久亚洲国产800 | 97人人草| 91精品国产91久久久久久不卞 | 日韩欧美亚洲 | 91se在线 | 国产高清久久 | 国产一区二区三区四区三区四 | 欧美一区二区在线播放 | 精品久久久久久久久久久久 | 欧美日韩精品综合 | 精品国产99| 中国黄色毛片视频 | 二区成人 | 91视频在线| 国产超碰人人爽人人做人人爱 | 日韩在线观看网站 | 日韩av一区二区在线观看 | 五月天综合影院 | 一区二区三区欧美在线 | 国内精品视频在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 国产伦精品一区二区三区高清 | 成人精品鲁一区一区二区 | 日韩在线免费电影 | 欧美在线一区二区三区 | 超碰成人免费 | 日韩视频国产 | 欧美一区二区三区在线 | 精品欧美乱码久久久久久1区2区 | 成人激情视频网 | 亚洲精品一区二区 | 久久国产香蕉 | 久久99深爱久久99精品 | 91精品国产一区二区 | 亚洲精品久久久一区二区三区 | 狠狠狠色丁香婷婷综合久久五月 |