在c中对进行操作

上传人:xiao****1972 文档编号:84035111 上传时间:2019-03-02 格式:DOC 页数:35 大小:172.17KB
返回 下载 相关 举报
在c中对进行操作_第1页
第1页 / 共35页
在c中对进行操作_第2页
第2页 / 共35页
在c中对进行操作_第3页
第3页 / 共35页
在c中对进行操作_第4页
第4页 / 共35页
在c中对进行操作_第5页
第5页 / 共35页
点击查看更多>>
资源描述

《在c中对进行操作》由会员分享,可在线阅读,更多相关《在c中对进行操作(35页珍藏版)》请在金锄头文库上搜索。

1、8.1 使用C#创建Word文档在常见的信息管理系统中,经常涉及文件的收发、数据的整理及报表功能。除了使用应用程序本身进行显示、处理之外,还必须考虑到企业原有的办公系统。由于大部分企业仍然以使用Word进行字处理为主,一般需要添加进行Word文档输出的功能。本部分介绍如何使用C#创建Word文档的方法。创建Word文档所使用的主要方法是通过微软公司提供的Microsoft Word X Object Library,其中X为版本号。Word 2007对应12.0,Word 2003对应11.0。通过在项目中添加该组件,即可使用微软公司提供的方法创建相应版本的Word文档。1目的说明介绍创建Wo

2、rd文档的基本知识,通过实例演示如何创建Word 2003版本的Word文档和Word 2007版本的Word文档。2操作步骤(1)创建一个Windows控制台应用程序,命名为CreateWordDemo。(2)添加引用,如图8.1所示。引用的库位于“COM”选项卡下,名称为Microsoft Word 12.0 Object Library。其中12.0是版本号,对应Microsoft Word 2007。Microsoft Word 2003对应的版本号为11.0。考虑到Microsoft Office 2007版本系列的软件能够比较方便地使用Microsoft Office 2003版本

3、系列创建的文档,本节首先使用Microsoft Word 11.0 Object Library创建一个Word 2003文档。添加后“解决方案资源管理器”面板的引用项中自动多出了三个引用,如图8.2所示。分别为Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE。 图8.1 添加引用 图8.2 “解决方案资源管理器”面板(3)在“Program.cs”文件中添加如下引用。using MSWord = Microsoft.Office.Interop.Word; /using MSWord = Microsoft.Office.In

4、terop.Word;经验证此行需要改正为using MSWord = Word;using System.IO;using System.Reflection;(4)直接修改“Program.cs”文件的代码如下。class Program static void Main(string args) object path; /文件路径变量 string strContent; /文本内容变量 MSWord.Application wordApp; /Word应用程序变量 MSWord.Document wordDoc; /Word文档变量 path = C:MyWord.doc; /路径

5、wordApp = new MSWord.ApplicationClass(); /初始化/ wordApp = new MSWord.ApplicationClass();经验证此行需要改正为wordApp = new MSWord.Application(); /如果已存在,则删除 if (File.Exists(string)path) File.Delete(string)path); /由于使用的是COM库,因此有许多变量需要用Missing.Value代替 Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add

6、(ref Nothing, ref Nothing, ref Nothing, ref Nothing); /WdSaveFormat为Word文档的保存格式 object format =MSWord.WdSaveFormat.wdFormatDocument; /将wordDoc文档对象的内容保存为DOC文档 wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, r

7、ef Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /关闭wordDoc文档对象 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); /关闭wordApp组件对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); Console.WriteLine(path + 创建完毕!); 3运行结果运行程序,结果如图8.3所示。打开C盘根目录,如图8.4所示。 图8.3 运行结果 图8.4

8、创建成功可以看到,已经成功地创建了一个名为MyWord.doc的Word文档。该文档是Microsoft Word 2003默认的文档格式,大小约为22KB。下面介绍如何使用其创建一个Microsoft Word 2007默认文档格式的Word文档。4目的说明在Microsoft.Office.Interop.Word命名空间下有一个枚举名为WdSaveFormat,设定了可用于保存的形式,如图8.5所示。对应于如图8.6所示的Word保存格式。 图8.5 WdSaveFormat枚举 图8.6 保存格式可以看到,WdSaveFormat枚举中定义的格式更为详细,下面介绍如何创建一个Micro

9、soft Word 2007格式的文档。5操作步骤(1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。(2)添加对Microsoft Word 12.0 Object Library的引用(同之前的步骤,不再详述)。(3)在“Program.cs”文件中添加如下引用。using MSWord = Microsoft.Office.Interop.Word;using System.IO;using System.Reflection;(4)直接修改“Program.cs”文件的代码如下。class Program static void Main(string a

10、rgs) object path; /文件路径变量 string strContent; /文本内容变量 MSWord.Application wordApp; /Word应用程序变量 MSWord.Document wordDoc; /Word文档变量 path = C:MyWord.docx; /路径 wordApp = new MSWord.ApplicationClass(); /初始化 /如果已存在,则删除 if (File.Exists(string)path) File.Delete(string)path); /由于使用的是COM库,因此有许多变量需要用Missing.Valu

11、e代替 Object Nothing = Missing.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); /strContent = 你好!n; /wordDoc.Paragraphs.Last.Range.Text = strContent; /strContent = Hello World; /wordDoc.Paragraphs.Last.Range.Text = strContent; /WdSaveFormat为Word 2007文档的保存格式 o

12、bject format =MSWord.WdSaveFormat.wdFormatDocumentDefault; /将wordDoc文档对象的内容保存为DOCX文档 wordDoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /关闭wordDoc文档对象 wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); /关闭wordApp组件对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); Console.WriteLine(path + 创建完毕!); 6运行结果运行程序,结果如

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号