成人免费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 10:25:14

C#操作Word

2009-08-19 11:28:41

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-26 13:48:31

C#打印條碼

2009-08-20 11:07:07

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技術棧公眾號

主站蜘蛛池模板: 国产高清不卡 | av在线影院| 日韩精品一区二区三区在线播放 | xxx国产精品视频 | 久久av一区二区三区 | 日韩国产中文字幕 | 成人欧美一区二区 | 国产高清一区二区 | 欧美aaa级 | 毛片网站在线观看视频 | 怡红院成人在线视频 | 国产精品日韩在线 | 国产精品久久网 | 欧美日韩在线观看一区 | 一区视频| 一级欧美| 亚洲二区在线 | 视频一区 亚洲 | 亚洲精品久久久一区二区三区 | 国产亚洲精品一区二区三区 | 成人免费看电影 | 91高清视频在线观看 | 国产欧美在线视频 | 国产综合久久久久久鬼色 | 久久国产精品99久久久大便 | 99在线视频观看 | 国产精品美女久久久久久免费 | 中文字幕一区二区在线观看 | a级大片免费观看 | 亚洲最大的黄色网址 | 欧美一区二区三区在线视频 | 美女国产一区 | 夜色www国产精品资源站 | 亚洲97 | 精品国产一区二区三区久久久蜜月 | 青青草一区 | 粉嫩一区二区三区国产精品 | 91在线观看视频 | 特黄特黄a级毛片免费专区 av网站免费在线观看 | 青青操av | 国产精品久久久久久久久久不蜜臀 |