C#操作Word实用实例:下面是我自己写的一段完整的代码,功能:在一个指定的Word文档中查找指定的关键字,并打印出包含该关键字的段落。使用的Range对象。现在让我们看看具体的实现过程:
成都创新互联公司 - 电信机房托管,四川服务器租用,成都服务器租用,四川网通托管,绵阳服务器托管,德阳服务器托管,遂宁服务器托管,绵阳服务器托管,四川云主机,成都云主机,西南云主机,电信机房托管,西南服务器托管,四川/成都大带宽,机柜大带宽,四川老牌IDC服务商
- 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实用实例的基本情况就向你介绍到这里,希望对你了解和学习C#操作Word有所帮助。
文章题目:C#操作Word实用实例浅析
文章地址:http://www.mswzjz.cn/qtweb/news28/158278.html
攀枝花网站建设、攀枝花网站运维推广公司-贝锐智能,是专注品牌与效果的网络营销公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 贝锐智能