资源预览内容
第1页 / 共35页
第2页 / 共35页
第3页 / 共35页
第4页 / 共35页
第5页 / 共35页
第6页 / 共35页
第7页 / 共35页
第8页 / 共35页
第9页 / 共35页
第10页 / 共35页
亲,该文档总共35页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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目的说明介绍创建Word文档的基本知识,通过实例演示如何创建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版本系列创建的文档,本节首先使用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.Interop.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; /路径 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(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, 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 + 创建完毕!); 3运行结果运行程序,结果如图8.3所示。打开C盘根目录,如图8.4所示。 图8.3 运行结果 图8.4 创建成功可以看到,已经成功地创建了一个名为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枚举中定义的格式更为详细,下面介绍如何创建一个Microsoft 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 args) 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.Value代替 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文档的保存格式 object 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运行结果运行程序,结果如
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号