溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C#操作Word怎么實現

發布時間:2021-12-02 11:39:15 來源:億速云 閱讀:186 作者:iii 欄目:編程語言

這篇文章主要介紹“C#操作Word怎么實現”,在日常操作中,相信很多人在C#操作Word怎么實現問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#操作Word怎么實現”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

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

using System;  using System.Collections;  using Word;   //C#操作Word實用實例namespace SearchWordDoc  {      /// summary﹥      /// SearchWordDo﹤c's summary      /// ﹤/summary﹥      public class SearchWordDoc      {     // search word in document.     // strName is the document name which is searched.     // strFind is the key word or phrase.     // return the match paragraphs.     public ArrayList swd(string strFName,   string strFind)     {    ArrayList textsFound = new ArrayList();     // matched texts    object missingValue = Type.Missing;    Word.ApplicationClass wdApp = null;   // Word Application object     //C#操作Word實用實例  try   {   object fName = strFName as object;   wdApp = new ApplicationClass();   // create a Word application object   Word.Document wdDoc = wdApp.Documents.Open(  ref fName, ref missingValue,  ref missingValue, ref missingValue,   ref missingValue,  ref missingValue, ref missingValue,   ref missingValue,  ref missingValue, ref missingValue,   ref missingValue,  ref missingValue, ref missingValue,   ref missingValue,  ref missingValue, ref missingValue);   // open a Word object    // the Word object has paragraphs or not   if (wdDoc.Paragraphs != null && wdDoc.Paragraphs.Count ﹥ 0)   {  int count = wdDoc.Paragraphs.Count;    // the number of doc paragraphs  Word.Range rng;  // Word.Range object  Word.Find fnd;   // Word.Find object   Console.WriteLine("There are {0}   paragraphs in document '{1}'.", count, strFName);   //C#操作Word實用實例for (int i = 1; i ﹤= count; ++ i)      // search key words in every paragraphs  {      rng = wdDoc.Paragraphs[i].Range;      fnd = rng.Find;      fnd.ClearFormatting();      fnd.Text = strFind;       if (fnd.Execute(ref missingValue,   ref missingValue,     ref missingValue, ref missingValue,   ref missingValue,     ref missingValue, ref missingValue,   ref missingValue,     ref missingValue, ref missingValue,   ref missingValue,     ref missingValue, ref missingValue,   ref missingValue,     ref missingValue))      {     // if find the matched paragrahps,   add it into the textsFound ArrayList.     textsFound.Add("[" + i.ToString() + "]   " + wdDoc.Paragraphs[i].Range.Text.Trim());      }  }   }  //C#操作Word實用實例  }    catch (NullReferenceException e)    {   Console.WriteLine(e.ToString());   wdApp.Quit(ref missingValue,   ref missingValue, ref missingValue);    }    catch (Exception e)    {   Console.WriteLine(e.ToString());   wdApp.Quit(ref missingValue,   ref missingValue, ref missingValue);    }     // release the Word application object    wdApp.Quit(ref missingValue,   ref missingValue, ref missingValue);     return textsFound;     }      // Display usage     public void usage()     {    Console.WriteLine("\nUsage:   SearchWordDoc doc_name string_found " +   "[start_paragraph_NO.]\n\t\t[end_paragraph_NO.]");      }  //C#操作Word實用實例    // Print the result     public void printText(ArrayList lst)     {    if (lst == null)    {   Console.WriteLine("Error: Null ArrayList.\n");   return;    }     int len = lst.Count;    for (int i = 0; i ﹤ len; ++ i)    {   Console.WriteLine("\t" + lst[i] as string);    }     Console.WriteLine("\nThere are {0} records.", len);     }      // Function Main     public static void Main(string[] args)     {    ArrayList textsFound = new ArrayList();    SearchWordDoc sobject = new SearchWordDoc();   //C#操作Word實用實例  switch (args.Length)    {   case 0:   case 1:  sobject.usage();  break;   case 2:  textsFound = sobject.swd(args[0], args[1]);  Console.WriteLine("Search Result:\n");  sobject.printText(textsFound);  break;   default:  sobject.usage();  break;    }     }      }  } // End

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

到此,關于“C#操作Word怎么實現”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女