ImageVerifierCode 换一换
格式:DOCX , 页数:51 ,大小:28.58KB ,
资源ID:11696946      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/11696946.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(C#操作office.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

C#操作office.docx

1、C#操作officeC#的office文档操作 收藏 Microsoft Office是微软公司推出的办公应用程序,主要包括Microsoft Word,Microsoft Excel、Microsoft Outlook和Microsoft Access等应用程序。提供了诸如字处理、表格处理、邮件处理和数据库等功能。目前被广泛使用的版本是Microsoft Office 2003和Microsoft Office 2007。作为微软公司推出的重量级编程语言,C#中提供了对大部分Office文件和应用的支持。本章主要介绍如何使用C#操作各类Office文件。8.1 使用C#创建Word文档在常见

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

3、建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版本系列创建的文档,本节首先使用Micro

4、soft 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 System.IO;using System.Reflection;(4)直接修改“Program

5、.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(); /初始化 /如果已存在,则删除 if (File.Exists(string)path) File.Delet

6、e(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,

7、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 Noth

8、ing, 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命名空间下有一个枚举名为WdSaveForma

9、t,设定了可用于保存的形式,如图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.In

10、terop.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.Applicat

11、ionClass(); /初始化 /如果已存在,则删除 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 = strC

12、ontent; /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 Nothi

13、ng, 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运行结果运行程

14、序,结果如图8.7所示。图8.7 运行结果打开C盘根目录,如图8.8所示。图8.8 创建成功可以看到,已经成功地创建了一个名为MyWord.docx的Word文档。该文档是Microsoft Word 2007默认的文档格式,大小约为11KB。8.2 使用C#向Word文档中写入文本文本是一个Word文档中最简单的元素,通过各种形式的文本与其他元素有机组合才形成了一个完整的Word文档。本节介绍如何使用C#向Word文档中写入文本信息。在向Word文档中写入文本时,仍然需要使用上节介绍的Microsoft Word X Object Library COM组件。写入文本的方法主要为设置MSWo

15、rd.Document.Paragraphs.Last.Range.Text属性,通过设置不同的字符串,即可达到写入文本的目的。1目的说明介绍如何向Word文档中写入文本和如何向Word文档中写入多行文本。2操作步骤(1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。(2)添加对Microsoft Word 12.0 Object Library的引用。(3)在“Program.cs”文件中添加如下引用。using MSWord = Microsoft.Office.Interop.Word;using System.IO;using System.Reflect

16、ion;(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(str

17、ing)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 = 使用C#向Word文档中写入文本n; wordDoc.Paragraphs.Last.Range.Text = strContent; strContent = 写入第二行文本; wordDo

18、c.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,

19、 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.9所示。图8.9 运行结果打开C盘根目录下的MyWord.docx,

20、如图8.10所示。图8.10 运行结果8.3 使用C#向Word输出格式化的文本一个Word文档不可能全部由无格式的普通文本组成,因此在从C#中向Word写入文本信息时经常要输出一些具有特殊字体、颜色的文本。本节介绍如何向Word输出格式化的文本。Microsoft Word X Object Library COM组件中文本格式的设置主要是由文本所使用的字体决定的。该COM组件中可以直接设置C#中的Font类,使用非常方便。常用的格式属性有颜色、加粗、斜体、下画线等。1目的说明分别介绍以下内容: 输出不同字体的文本。 输出不同颜色的文本。 输出带下画线的文本。 输出斜体文本。 输出加粗文本。

21、2操作步骤(1)创建一个Windows控制台应用程序,命名为CreateFormatWordDemo。(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; /文件路径变量 st

22、ring 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

23、.Value; wordDoc = wordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); /写入普通文本 strContent = 普通文本普通文本普通文本普通文本普通文本n; wordDoc.Paragraphs.Last.Range.Text = strContent; /写入黑体文本 strContent = 黑体文本黑体文本黑体文本黑体文本黑体文本n; wordDoc.Paragraphs.Last.Range.Font.Name = 黑体; wordDoc.Paragraphs.Last.

24、Range.Text = strContent; /写入加粗文本 strContent = 加粗文本加粗文本加粗文本加粗文本加粗文本n; wordDoc.Paragraphs.Last.Range.Font.Bold = 1; wordDoc.Paragraphs.Last.Range.Text = strContent; /写入15号字体文本 strContent = 15号字体文本15号字体文本15号字体文本15号字体文本n; wordDoc.Paragraphs.Last.Range.Font.Size = 15; wordDoc.Paragraphs.Last.Range.Text =

25、 strContent; /写入斜体文本 strContent = 斜体文本斜体文本斜体文本斜体文本斜体文本n; wordDoc.Paragraphs.Last.Range.Font.Italic = 1; wordDoc.Paragraphs.Last.Range.Text = strContent; /写入蓝色文本 strContent = 蓝色文本蓝色文本蓝色文本蓝色文本蓝色文本n; wordDoc.Paragraphs.Last.Range.Font.Color = MSWord.WdColor.wdColorBlue; wordDoc.Paragraphs.Last.Range.Te

26、xt = strContent; /写入下画线文本 strContent = 下画线文本下画线文本下画线文本下画线文本下画线文本n; wordDoc.Paragraphs.Last.Range.Font.Underline = MSWord.WdUnderline.wdUnderlineThick; wordDoc.Paragraphs.Last.Range.Text = strContent; /写入红色下画线文本 strContent = 红色下画线文本红色下画线文本红色下画线文本红色下画线文本n; wordDoc.Paragraphs.Last.Range.Font.Underline

27、= MSWord.WdUnderline.wdUnderlineThick; wordDoc.Paragraphs.Last.Range.Font.UnderlineColor = MSWord.WdColor.wdColorRed; wordDoc.Paragraphs.Last.Range.Text = strContent; /WdSaveFormat为Word 2007文档的保存格式 object format =MSWord.WdSaveFormat.wdFormatDocumentDefault; /将wordDoc文档对象的内容保存为DOCX文档 wordDoc.SaveAs(r

28、ef 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

29、(ref Nothing, ref Nothing, ref Nothing); Console.WriteLine(path + 创建完毕!); 3运行结果运行程序,结果如图8.11所示。打开C盘根目录下的MyWord.docx,如图8.12所示。 图8.11 运行结果 图8.12 运行结果8.4 使用C#向Word文档中添加表格除了简单的文本信息外,Microsoft Word也是一个处理表格的强大工具。许多数据报表也需要通过表格的形式在Word中体现。本节将介绍如何使用C#在Word中创建一个表格。表格是由Microsoft Word X Object Library中的MSWord.T

30、able定义的,通过在Word文档中的Tables集合中添加一个Table对象,即可在Word文档中创建一个表格。该表格的行数和列数等属性都可以在Tables的Add方法中定义,表格的内容可由Cell属性访问。1目的说明介绍如何向Word文档中输出表格和如何向Word文档中的表格填充文本。2操作步骤(1)创建一个Windows控制台应用程序,命名为CreateTableDemo。(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文档变量

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1