C#操作office.docx
《C#操作office.docx》由会员分享,可在线阅读,更多相关《C#操作office.docx(51页珍藏版)》请在冰豆网上搜索。
C#操作office
C#的office文档操作收藏
MicrosoftOffice是微软公司推出的办公应用程序,主要包括MicrosoftWord,MicrosoftExcel、MicrosoftOutlook和MicrosoftAccess等应用程序。
提供了诸如字处理、表格处理、邮件处理和数据库等功能。
目前被广泛使用的版本是MicrosoftOffice2003和MicrosoftOffice2007。
作为微软公司推出的重量级编程语言,C#中提供了对大部分Office文件和应用的支持。
本章主要介绍如何使用C#操作各类Office文件。
8.1使用C#创建Word文档
在常见的信息管理系统中,经常涉及文件的收发、数据的整理及报表功能。
除了使用应用程序本身进行显示、处理之外,还必须考虑到企业原有的办公系统。
由于大部分企业仍然以使用Word进行字处理为主,一般需要添加进行Word文档输出的功能。
本部分介绍如何使用C#创建Word文档的方法。
创建Word文档所使用的主要方法是通过微软公司提供的MicrosoftWordXObjectLibrary,其中X为版本号。
Word2007对应12.0,Word2003对应11.0。
通过在项目中添加该组件,即可使用微软公司提供的方法创建相应版本的Word文档。
1.目的说明
介绍创建Word文档的基本知识,通过实例演示如何创建Word2003版本的Word文档和Word2007版本的Word文档。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateWordDemo。
(2)添加引用,如图8.1所示。
引用的库位于“COM”选项卡下,名称为MicrosoftWord12.0ObjectLibrary。
其中12.0是版本号,对应MicrosoftWord2007。
MicrosoftWord2003对应的版本号为11.0。
考虑到MicrosoftOffice2007版本系列的软件能够比较方便地使用MicrosoftOffice2003版本系列创建的文档,本节首先使用MicrosoftWord11.0ObjectLibrary创建一个Word2003文档。
添加后“解决方案资源管理器”面板的引用项中自动多出了三个引用,如图8.2所示。
分别为Microsoft.Office.Core、Microsoft.Office.Interop.Word和VBIDE。
图8.1添加引用图8.2“解决方案资源管理器”面板
(3)在“Program.cs”文件中添加如下引用。
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
classProgram
{
staticvoidMain(string[]args)
{
objectpath;//文件路径变量
stringstrContent;//文本内容变量
MSWord.ApplicationwordApp;//Word应用程序变量
MSWord.DocumentwordDoc;//Word文档变量
path=@"C:
\MyWord.doc";//路径
wordApp=newMSWord.ApplicationClass();//初始化
//如果已存在,则删除
if(File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
ObjectNothing=Missing.Value;
wordDoc=wordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
//WdSaveFormat为Word文档的保存格式
objectformat=MSWord.WdSaveFormat.wdFormatDocument;
//将wordDoc文档对象的内容保存为DOC文档
wordDoc.SaveAs(refpath,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
//关闭wordDoc文档对象
wordDoc.Close(refNothing,refNothing,refNothing);
//关闭wordApp组件对象
wordApp.Quit(refNothing,refNothing,refNothing);
Console.WriteLine(path+"创建完毕!
");
}
}
3.运行结果
运行程序,结果如图8.3所示。
打开C盘根目录,如图8.4所示。
图8.3运行结果图8.4创建成功
可以看到,已经成功地创建了一个名为MyWord.doc的Word文档。
该文档是MicrosoftWord2003默认的文档格式,大小约为22KB。
下面介绍如何使用其创建一个MicrosoftWord2007默认文档格式的Word文档。
4.目的说明
在Microsoft.Office.Interop.Word命名空间下有一个枚举名为WdSaveFormat,设定了可用于保存的形式,如图8.5所示。
对应于如图8.6所示的Word保存格式。
图8.5WdSaveFormat枚举图8.6保存格式
可以看到,WdSaveFormat枚举中定义的格式更为详细,下面介绍如何创建一个MicrosoftWord2007格式的文档。
5.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。
(2)添加对MicrosoftWord12.0ObjectLibrary的引用(同之前的步骤,不再详述)。
(3)在“Program.cs”文件中添加如下引用。
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
classProgram
{
staticvoidMain(string[]args)
{
objectpath;//文件路径变量
stringstrContent;//文本内容变量
MSWord.ApplicationwordApp;//Word应用程序变量
MSWord.DocumentwordDoc;//Word文档变量
path=@"C:
\MyWord.docx";//路径
wordApp=newMSWord.ApplicationClass();//初始化
//如果已存在,则删除
if(File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
ObjectNothing=Missing.Value;
wordDoc=wordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
//strContent="你好!
\n";
//wordDoc.Paragraphs.Last.Range.Text=strContent;
//strContent="HelloWorld";
//wordDoc.Paragraphs.Last.Range.Text=strContent;
//WdSaveFormat为Word2007文档的保存格式
objectformat=MSWord.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(refpath,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
//关闭wordDoc文档对象
wordDoc.Close(refNothing,refNothing,refNothing);
//关闭wordApp组件对象
wordApp.Quit(refNothing,refNothing,refNothing);
Console.WriteLine(path+"创建完毕!
");
}
}
6.运行结果
运行程序,结果如图8.7所示。
图8.7运行结果
打开C盘根目录,如图8.8所示。
图8.8创建成功
可以看到,已经成功地创建了一个名为MyWord.docx的Word文档。
该文档是MicrosoftWord2007默认的文档格式,大小约为11KB。
8.2使用C#向Word文档中写入文本
文本是一个Word文档中最简单的元素,通过各种形式的文本与其他元素有机组合才形成了一个完整的Word文档。
本节介绍如何使用C#向Word文档中写入文本信息。
在向Word文档中写入文本时,仍然需要使用上节介绍的MicrosoftWordXObjectLibraryCOM组件。
写入文本的方法主要为设置MSWord.Document.Paragraphs.Last.Range.Text属性,通过设置不同的字符串,即可达到写入文本的目的。
1.目的说明
介绍如何向Word文档中写入文本和如何向Word文档中写入多行文本。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateWordXDemo。
(2)添加对MicrosoftWord12.0ObjectLibrary的引用。
(3)在“Program.cs”文件中添加如下引用。
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
classProgram
{
staticvoidMain(string[]args)
{
objectpath;//文件路径变量
stringstrContent;//文本内容变量
MSWord.ApplicationwordApp;//Word应用程序变量
MSWord.DocumentwordDoc;//Word文档变量
path=@"C:
\MyWord.docx";//路径
wordApp=newMSWord.ApplicationClass();//初始化
//如果已存在,则删除
if(File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
ObjectNothing=Missing.Value;
wordDoc=wordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
strContent="使用C#向Word文档中写入文本\n";
wordDoc.Paragraphs.Last.Range.Text=strContent;
strContent="写入第二行文本";
wordDoc.Paragraphs.Last.Range.Text=strContent;
//WdSaveFormat为Word2007文档的保存格式
objectformat=MSWord.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(refpath,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
//关闭wordDoc文档对象
wordDoc.Close(refNothing,refNothing,refNothing);
//关闭wordApp组件对象
wordApp.Quit(refNothing,refNothing,refNothing);
Console.WriteLine(path+"创建完毕!
");
}
}
3.运行结果
运行程序,结果如图8.9所示。
图8.9运行结果
打开C盘根目录下的MyWord.docx,如图8.10所示。
图8.10运行结果
8.3使用C#向Word输出格式化的文本
一个Word文档不可能全部由无格式的普通文本组成,因此在从C#中向Word写入文本信息时经常要输出一些具有特殊字体、颜色的文本。
本节介绍如何向Word输出格式化的文本。
MicrosoftWordXObjectLibraryCOM组件中文本格式的设置主要是由文本所使用的字体决定的。
该COM组件中可以直接设置C#中的Font类,使用非常方便。
常用的格式属性有颜色、加粗、斜体、下画线等。
1.目的说明
分别介绍以下内容:
—输出不同字体的文本。
—输出不同颜色的文本。
—输出带下画线的文本。
—输出斜体文本。
—输出加粗文本。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateFormatWordDemo。
(2)添加对MicrosoftWord12.0ObjectLibrary的引用。
(3)在“Program.cs”文件中添加如下引用。
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
classProgram
{
staticvoidMain(string[]args)
{
objectpath;//文件路径变量
stringstrContent;//文本内容变量
MSWord.ApplicationwordApp;//Word应用程序变量
MSWord.DocumentwordDoc;//Word文档变量
path=@"C:
\MyWord.docx";//路径
wordApp=newMSWord.ApplicationClass();//初始化
//如果已存在,则删除
if(File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Missing.Value代替
ObjectNothing=Missing.Value;
wordDoc=wordApp.Documents.Add(refNothing,refNothing,refNothing,refNothing);
//写入普通文本
strContent="普通文本普通文本普通文本普通文本普通文本\n";
wordDoc.Paragraphs.Last.Range.Text=strContent;
//写入黑体文本
strContent="黑体文本黑体文本黑体文本黑体文本黑体文本\n";
wordDoc.Paragraphs.Last.Range.Font.Name="黑体";
wordDoc.Paragraphs.Last.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=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.Text=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=MSWord.WdUnderline.wdUnderlineThick;
wordDoc.Paragraphs.Last.Range.Font.UnderlineColor=MSWord.WdColor.wdColorRed;
wordDoc.Paragraphs.Last.Range.Text=strContent;
//WdSaveFormat为Word2007文档的保存格式
objectformat=MSWord.WdSaveFormat.wdFormatDocumentDefault;
//将wordDoc文档对象的内容保存为DOCX文档
wordDoc.SaveAs(refpath,refformat,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing,refNothing);
//关闭wordDoc文档对象
wordDoc.Close(refNothing,refNothing,refNothing);
//关闭wordApp组件对象
wordApp.Quit(refNothing,refNothing,refNothing);
Console.WriteLine(path+"创建完毕!
");
}
}
3.运行结果
运行程序,结果如图8.11所示。
打开C盘根目录下的MyWord.docx,如图8.12所示。
图8.11运行结果图8.12运行结果
8.4使用C#向Word文档中添加表格
除了简单的文本信息外,MicrosoftWord也是一个处理表格的强大工具。
许多数据报表也需要通过表格的形式在Word中体现。
本节将介绍如何使用C#在Word中创建一个表格。
表格是由MicrosoftWordXObjectLibrary中的MSWord.Table定义的,通过在Word文档中的Tables集合中添加一个Table对象,即可在Word文档中创建一个表格。
该表格的行数和列数等属性都可以在Tables的Add方法中定义,表格的内容可由Cell属性访问。
1.目的说明
介绍如何向Word文档中输出表格和如何向Word文档中的表格填充文本。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateTableDemo。
(2)添加对MicrosoftWord12.0ObjectLibrary的引用。
(3)在“Program.cs”文件中添加如下引用。
usingMSWord=Microsoft.Office.Interop.Word;
usingSystem.IO;
usingSystem.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
classProgram
{
staticvoidMain(string[]args)
{
objectpath;//文件路径变量
stringstrContent;//文本内容变量
MSWord.ApplicationwordApp;//Word应用程序变量
MSWord.DocumentwordDoc;//Word文档变量