C#操作Word怎么实现
这篇文章主要介绍“C#操作Word怎么实现”,在日常操作中,相信很多人在C#操作Word怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C#操作Word怎么实现”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
C#操作Word实用实例:下面是我自己写的一段完整的代码,功能:在一个指定的Word文档中查找指定的关键字,并打印出包含该关键字的段落。使用的Range对象。现在让我们看看具体的实现过程:
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怎么实现”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!