C#操作Word怎么实现
发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,这篇文章主要介绍"C#操作Word怎么实现",在日常操作中,相信很多人在C#操作Word怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C#操作Word怎么实现"
千家信息网最后更新 2026年11月03日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; } } } } // EndC#对Word的操作和对Excel等的操作方法很相似。
到此,关于"C#操作Word怎么实现"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
实用
实例
C#
学习
关键
关键字
方法
更多
帮助
相似
接下来
代码
功能
对象
文档
文章
段落
理论
知识
篇文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
广州为想互联网科技有限公司
西安纸贵互联网科技有限公司
互联网核心科技是什么
互联网巨头反垄断金融科技
美国互联网科技巨头监管
重庆互联网络科技
广州宏创互联网科技
贷财互联网科技
互联网科技公司有哪些
互联网科技产品
深圳育鹏互联网科技有限公司招聘
银川新华互联网科技学校联系电话
特约风豹互联网科技为什么扣费
2021互联网科技公司排名
山东星卡互联网科技有限公司官网
杭州披云加互联网科技有限公司
厦门江牛科技互联网
普陀区互联网教育科技管理办法
唯美互联网科技
广西泛欧互联网科技有限公司
新华互联网科技学院
万易互联网科技
上海万想互联网科技
长亮科技互联网金融
西藏七维空间互联网科技有限公司
速维有品互联网信息科技(苏州)有限公司
洛阳昕诺飞互联网科技有限公司
互联网科技数字货币
互联网科技博主张洲
上海旺乐尼互联网科技有限公司