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

C#的office文档操作(5)

 
阅读更多
8.7 使用C#向Excel文档中写入数据
Microsoft Excel的强大功能就在于其数据处理功能,用户可以通过其方便的操作和强大的公式和图表处理现有的数据。本节介绍如何使用C#向Excel文档中写入数据。
在Excel文档中,数据是有明确的标识的,一般由其行名称和列名称进行标识。在C#中 向Excel文档写入数据时,Microsoft Excel X Object Library也提供了这种支持。只需明确地给出所需添加的位置,即可向Excel文档的指定位置添加数据。
1.目的说明
本实例介绍的知识点为如何向Excel文档中输出数据和如何设定输出数据的位置。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为WriteExcelDemo。
(2)添加对Microsoft Excel 12.0 Object Library的引用。
(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);
//使用第一个工作表作为插入数据的工作表
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];
//声明一个MSExcel.Range 类型的变量r
MSExcel.Range r;
//获得A1处的表格,并赋值
r = ws.get_Range("A1", "A1");
r.Value2 = "数据1";
//获得A2处的表格,并赋值
r = ws.get_Range("A2", "A2");
r.Value2 = "5.7";
//WdSaveFormat为Excel文档的保存格式
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
//将excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, format, 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.26所示。
图8.26 运行结果
打开C盘根目录下的MyExcel.xlsx文件,如图8.27所示。
图8.27 创建成功
8.8 使用C#在Excel文档中创建图表
图表功能是Excel中一项非常强大的功能,能将表格中的数据生成直观的图表,便于观看。本节介绍如何使用C#在Excel文档中创建图表。
若要在C#中向Excel文档添加图表,首先需要添加图表的支持数据。图表的形成是以数 据为基础的,因此首先需要使用上节介绍的方法添加部分数据。然后根据数据向MSExcel.Workbook.Charts集合中添加图表,使用的是 MSExcel.Workbook.Charts.Add方法。图表的样式由MSExcel.XlChartType枚举指定,名称由 ChartTitle.Text属性指定。
1.目的说明
本实例主要介绍以下内容:
— 如何向Excel文档中输出多个数据。
— 如何向Excel文档中输出图表。
— 如何设定图表的名称。
— 如何设定图表的样式。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为WriteExcelDemo。
(2)添加对Microsoft Excel 12.0 Object Library的引用。
(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);
//使用第一个工作表作为插入数据的工作表
MSExcel.Worksheet ws = (MSExcel.Worksheet)excelDoc.Sheets[1];
//声明一个MSExcel.Range 类型的变量r
MSExcel.Range r;
//获得A1处的表格,并赋值
r = ws.get_Range("A1", "A1");
r.Value2 = "3";
//获得A2处的表格,并赋值
r = ws.get_Range("A2", "A2");
r.Value2 = "5.7";
//获得A3处的表格,并赋值
r = ws.get_Range("A3", "A3");
r.Value2 = "4.8";
//获得A4处的表格,并赋值
r = ws.get_Range("A4", "A4");
r.Value2 = "9.2";
//获得A5处的表格,并赋值
r = ws.get_Range("A5", "A5");
r.Value2 = "6.4";
excelDoc.Charts.Add(Nothing, Nothing, Nothing, Nothing);
excelDoc.ActiveChart.ChartType = MSExcel.XlChartType.xlColumnClustered;
excelDoc.ActiveChart.SetSourceData(ws.get_Range("A1", "A5"), MSExcel.XlRowCol.xlColumns);
excelDoc.ActiveChart.Location(MSExcel.XlChartLocation.xlLocationAsObject, "sheet1");
excelDoc.ActiveChart.HasTitle = true;
excelDoc.ActiveChart.ChartTitle.Text = "创建图表";
excelDoc.ActiveChart.HasDataTable = false;
//WdSaveFormat为Excel文档的保存格式
object format = MSExcel.XlFileFormat.xlWorkbookDefault;
//将excelDoc文档对象的内容保存为XLSX文档
excelDoc.SaveAs(path, format, 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.28所示。
图8.28 运行结果
打开C盘根目录下的MyExcel.xlsx文件,如图8.29所示。
图8.29 创建成功
8.9 使用C#创建PowerPoint文档
Microsoft PowerPoint是Microsoft Office的一个组件,是一款功能强大的演示文稿处理软件,它与其他软件的差别在于其能够创建精美的演示性文档,并且有条理地显示结果。 Microsoft PowerPoint是除了Microsoft Word和Microsoft Excel之外最常用的办公软件之一。本节将介绍如何使用C#创建PowerPoint文档。
与在C#中添加Word、Excel文档的方法类似,添加PowerPoint文档时, 需要为项目添加对Microsoft PowerPoint X Object Library的引用,其中的X对应为版本号。PowerPoint 2007对应12.0。在Microsoft PowerPoint X Object Library中,一个PowerPoint文档由MSPowerPoint.Presentations表示。
1.目的说明
本实例介绍的知识点为如何创建PowerPoint文档,以及如何使用不同格式保存创建的PowerPoint文档。
2.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreatePptDemo。
(2)添加引用,如图8.30所示。
图8.30 添加引用 图8.31 “解决方案资源管理器”面板
引用的库位于“COM”选项卡下,名称为Microsoft PowerPoint X Object Library。其中X是版本号,12.0对应Microsoft PowerPoint 2007。Microsoft PowerPoint 2003对应的版本号为11.0。本节使用Microsoft Excel 11.0 Object Library。
添加后“解决方案资源管理器”面板的引用项中自动多出了三个引用,如图8.31所示。分别为Microsoft.Office.Core、Microsoft.Office.Interop. PowerPoint和VBIDE。
(3)在“Program.cs”文件中添加如下引用。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
namespace CreatePptDemo
{
class Program
{
static void Main(string[] args)
{
string path; //文件路径变量
PPT.Application pptApp; //Excel应用程序变量
PPT.Presentation pptDoc; //Excel文档变量
path = @"C:/MyPPT.ppt"; //路径
pptApp = new PPT.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Nothing代替
Object Nothing = Missing.Value;
pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
//WdSaveFormat为Excel文档的保存格式
PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsDefault;
//将excelDoc文档对象的内容保存为XLSX文档
pptDoc.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);
//关闭excelDoc文档对象
pptDoc.Close();
//关闭excelApp组件对象
pptApp.Quit();
Console.WriteLine(path + " 创建完毕!");
Console.ReadLine();
}
}
}
3.运行结果
运行程序,结果如图8.32所示。
打开C盘根目录,如图8.33所示。
图8.32 运行结果 图8.33 创建成功
可以看到,已经成功地创建了一个名为MyPPT.ppt的PowerPoint文档。该 文档是Microsoft PowerPoint 2003默认的文档格式,大小约为25KB。下面介绍如何使用其创建一个在Microsoft PowerPoint 2003中可以识别的其他格式的PowerPoint文档。
在Microsoft.Office.Interop.PowerPoint命名空间下有一个枚举名为PpSaveAsFileFormat,设置了可用于保存的形式,如图8.34所示,对应于图8.35所示的保存格式。
图8.34 PpSaveAsFileFormat枚举 图8.35 保存格式
可以看到,PpSaveAsFileFormat枚举中定义的格式更为详细。下面将介绍如何创建一个PPS格式的文档。
1.操作步骤
(1)创建一个Windows控制台应用程序,命名为CreatePPSDemo。
(2)添加对Microsoft Excel 11.0 Object Library的引用(同之前的步骤,不再详述)。
(3)在“Program.cs”文件中添加如下引用。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Reflection;
(4)直接修改“Program.cs”文件的代码如下。
namespace CreatePpsDemo
{
class Program
{
static void Main(string[] args)
{
string path; //文件路径变量
PPT.Application pptApp; //Excel应用程序变量
PPT.Presentation pptDoc; //Excel文档变量
path = @"C:/MyPPT.pps"; //路径
pptApp = new PPT.ApplicationClass(); //初始化
//如果已存在,则删除
if (File.Exists((string)path))
{
File.Delete((string)path);
}
//由于使用的是COM库,因此有许多变量需要用Nothing代替
Object Nothing = Missing.Value;
pptDoc = pptApp.Presentations.Add(Microsoft.Office.Core.MsoTriState.msoFalse);
//WdSaveFormat为Excel文档的保存格式
PPT.PpSaveAsFileType format = PPT.PpSaveAsFileType.ppSaveAsShow;
//将excelDoc文档对象的内容保存为XLSX文档
pptDoc.SaveAs(path, format, Microsoft.Office.Core.MsoTriState.msoFalse);
//关闭excelDoc文档对象
pptDoc.Close();
//关闭excelApp组件对象
pptApp.Quit();
Console.WriteLine(path + " 创建完毕!");
Console.ReadLine();
}
}
}
2.运行结果
运行程序,结果如图8.36所示。
打开C盘根目录,如图8.37所示。
图8.36 运行结果 图8.37 创建成功
可以看到,已经成功地创建了一个名为MyPPT.pps的PPS文档。该文档是Microsoft PowerPoint 2003支持的文档格式,大小约为25KB。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics