C#操作Word怎么实现
发表于:2025-11-09 作者:千家信息网编辑
千家信息网最后更新 2025年11月09日,这篇文章主要介绍"C#操作Word怎么实现",在日常操作中,相信很多人在C#操作Word怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"C#操作Word怎么实现"
千家信息网最后更新 2025年11月09日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安全错误
数据库的锁怎样保障安全
熵增定律与数据库的关系
成都软件开发工作室哪家好
婴儿类app软件开发
怎样进入第五人格服务器
潜伏的网络安全吗
信息化网络安全目标
海南酷动网络技术有限公司
网络审计数据库审计
数据库数据空洞
江西警察学院网络安全与执法
盘龙区创新软件开发市场价
香港服务器托管有什么用
碳交易软件开发公司
数据库 备案
手机刷机显示访问服务器出差错
泰山科技产业园软件开发
出入境边防检查总站网络安全
索尼电视如何连接网络服务器
湖南潮流软件开发服务
服务器机箱风扇转速控制
众恒软件链接不到数据库
网络技术公关案例
网络安全意识的基本内容
幻塔海嘉德服务器人多么
津市市瓦力网络技术有限公司
win7怎样打开数据库
易语言数据库爆破工具
内网如何连接服务器
17年网络安全状况
网络连接突然变成服务器