`
kanwoerzi
  • 浏览: 1643229 次
文章分类
社区版块
存档分类
最新评论

C#的office文档操作(4)

 
阅读更多
8.5 使用C#向Word文档中插入图片
要想创建一个完整美观的Word文档,图片是必不可少的。本节将要介绍的内容就是如何从C#中向Word文档中写入一个图片文件。
在COM组件Microsoft Word X Object Library中,图片是由MSWord.Document. InlineShapes.AddPicture负责添加的,而没有单独表示图片的对象。只需用AddPicture方法给出图片的物理地址及一些简单的 属性即可向Word文档中添加图片。
1.目的说明
本实例介绍的知识点为如何向Word文档中输出图片。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreatePicDemo。
(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);
//图片文件的路径
string filename = @"C:/BackgroundImage.jpg";
//要向Word文档中插入图片的位置
Object range = wordDoc.Paragraphs.Last.Range;
//定义该插入的图片是否为外部链接
Object linkToFile = false; //默认
//定义要插入的图片是否随Word文档一起保存
Object saveWithDocument = true; //默认
//使用InlineShapes.AddPicture方法插入图片
wordDoc.InlineShapes.AddPicture(filename, ref linkToFile, ref saveWithDocument, ref range);
//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 + " 创建完毕!");
}
}
3.运行结果
运行程序,结果如图8.15所示。
图8.15 运行结果
打开C盘根目录下的MyWord.docx,如图8.16所示。
图8.16 运行结果
8.6 使用C#创建Excel文档
Microsoft Excel是Microsoft Office的一个组件,是功能强大的电子表格处理软件,它与文本处理软件的差别在于它能够运算复杂的公式,并且有条理地显示结果。Microsoft Excel是除了Microsoft Word之外最常用的办公软件之一,本节将介绍如何使用C#创建Excel文档。
与在C#中添加Word文档的方法类似,添加Excel文档时 需要为项目添加对Microsoft Excel X Object Library的引用,其中的X对应为版本号。Excel 2007对应12.0。在Microsoft Excel X Object Library中,一个Excel文档由MSExcel.Workbook表示。
1.目的说明
本实例介绍的知识点为如何创建Excel文档和如何使用不同格式保存创建的Excel文档。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateWordDemo。
(2)添加引用,如图8.17所示。
引用的库位于“COM”选项卡下,名称为Microsoft Excel 12.0 Object Library。其中12.0是版本号,对应Microsoft Excel 2007。Microsoft Excel 2003对应的版本号为11.0。本节使用Microsoft Excel 12.0 Object Library。
添加后在“解决方案资源管理器”面板的引用项中自动多出了三个引用,如图8.18所示。分别为Microsoft.Office.Core、Microsoft.Office.Interop. Excel和VBIDE。
图8.17 添加引用 图8.18 “解决方案资源管理器”面板
(3)在“Program.cs”文件中添加如下引用。
using MSExcel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
class Program
{
static void Main(string[] args)
{
object path; //文件路径变量
MSExcel.Application excelApp; //Excel应用程序变量
MSExcel.Workbook excelDoc; //Excel文档变量
path = @"C:/MyExcel.xlsx"; //路径
excelApp = new MSExcel.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Nothing代替
Object Nothing = Missing.Value;
excelDoc = excelApp.Workbooks.Add(Nothing);
//WdSaveFormat为Excel文档的保存格式
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
//将excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, Nothing, Nothing, Nothing, Nothing, Nothing, MSExcel.XlSaveAsAccessMode.xlExclusive, Nothing, Nothing, Nothing, Nothing, Nothing);
//关闭excelDoc文档对象
excelDoc.Close(Nothing, Nothing, Nothing);
//关闭excelApp组件对象
excelApp.Quit();
Console.WriteLine(path + " 创建完毕!");
}
}
3.运行结果
运行程序,结果如图8.19所示。
打开C盘根目录,如图8.20所示。
图8.19 运行结果 图8.20 创建成功
可以看到,已经成功地创建了一个名为MyExcel.xlsx 的Excel文档。该文档是Microsoft Excel 2007默认的文档格式,大小约为8KB。下面介绍如何使用其创建一个Microsoft Excel 2007中可以识别的其他格式的Excel文档。
在Microsoft.Office.Interop.Excel命名空间下有一个枚举名为XlFileFormat,设定了可用于保存的形式,如图8.21所示,对应于如图8.22所示的Excel保存格式。
可以看到,XlFileFormat枚举中定义的格式更为详细,下面介绍如何创建一个CSV格式的文档。
4.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreateCSVDemo。
(2)添加对Microsoft Excel 12.0 Object Library的引用(同之前的步骤,不再详述)。
(3)在“Program.cs”文件中添加如下引用。
using MSExcel = Microsoft.Office.Interop. Excel;
using System.IO;
using System.Reflection;
图8.21 XlFileFormat枚举 图8.22 保存格式
(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 + " 创建完毕!");
}
}
5.运行结果
运行程序,结果如图8.23所示。
单击“是”按钮,如图8.24所示。
图8.23 运行提示 图8.24 运行结果
打开C盘根目录,如图8.25所示。
可以看到,已经成功创建了一个名为MyExcel.csv的CSV文档。该文档是Microsoft Excel 2007支持的文档格式,大小约为8KB。
图8.25 创建成功
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics