C#操作Word實用實例淺析
作者:佚名
C#操作Word實用實例主要向你介紹了一個實現(xiàn)功能:在一個指定的Word文檔中查找指定的關(guān)鍵字,并打印出包含該關(guān)鍵字的段落的C#操作Word實用實例。
C#操作Word實用實例:下面是我自己寫的一段完整的代碼,功能:在一個指定的Word文檔中查找指定的關(guān)鍵字,并打印出包含該關(guān)鍵字的段落。使用的Range對象。現(xiàn)在讓我們看看具體的實現(xiàn)過程:
- 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實用實例的基本情況就向你介紹到這里,希望對你了解和學(xué)習(xí)C#操作Word有所幫助。
【編輯推薦】
責(zé)任編輯:仲衡
來源:
ieee.org.cn