PPT控件Spire.Presentation 教程:在C#,VB.NET中将Excel对象嵌入PPT幻灯片

有时候我们需要在Excel幻灯片放映中呈现Excel数据,而不是切换到Excel,为此,可以将Excel数据以其他格式(如工作表对象,HTML格式,位图,图片或文本格式)粘贴到PowerPoint幻灯片。 在这篇文章中,将会给大家介绍如何将Excel工作表作为一个OLE对象插入PowerPoint中的C#,VB.NET中。

首先,我们必须使用Spire.XLS 来加载Excel文件并获取数据,然后使用Spire.Presentation 创建OEL对象。 所以请记得下载Spire.Office,并将所提到的dll添加为VS项目的参考。 然后按照以下指导完成这项工作。

Step 1:创建一个新的Workbook实例并加载Excel文件。

Workbook book = new Workbook();book.LoadFromFile(@"data.xlsx");

Step 2:从第一个工作表中选择单元格范围,并保存为图像。

mage image = book.Worksheets[0].ToImage(1, 1, 5, 4);

Step 3:初始化Presentation类的新实例,将图像添加到演示文稿中。

Presentation ppt = new Presentation();IImageData oleImage = ppt.Images.Append(image);

Step 4:根据Excel数据将OLE对象插入到演示文稿中。

Rectangle rec = new Rectangle(60,60,image.Width,image.Height);using (MemoryStream ms = new MemoryStream()){    book.SaveToStream(ms);    ms.Position = 0;    Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;    oleObject.ProgId = "Excel.Sheet.8";

Step 5:以指定的格式保存PowerPoint文件。

ppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007);

输出:

图片1

详细代码:

[C#]

Workbook book = new Workbook();book.LoadFromFile(@"data.xlsx");Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);Presentation ppt = new Presentation();IImageData oleImage = ppt.Images.Append(image);Rectangle rec = new Rectangle(60,60,image.Width,image.Height);using (MemoryStream ms = new MemoryStream()){    book.SaveToStream(ms);    ms.Position = 0;    Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;    oleObject.ProgId = "Excel.Sheet.8";}ppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007);}

[VB.NET]

Dim book As New Workbook()book.LoadFromFile("data.xlsx")Dim image As Image = book.Worksheets(0).ToImage(1, 1, 5, 4)Dim ppt As New Presentation()Dim oleImage As IImageData = ppt.Images.Append(image)Dim rec As New Rectangle(60, 60, image.Width, image.Height)Using ms As New MemoryStream()    book.SaveToStream(ms)    ms.Position = 0    Dim oleObject As Spire.Presentation.IOleObject = ppt.Slides(0).Shapes.AppendOleObject("excel", ms.ToArray(), rec)    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage    oleObject.ProgId = "Excel.Sheet.8";End Usingppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007)

控件

标签:文档管理PPTExcel数据文档处理

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2017年7月2日
下一篇 2017年7月2日

相关推荐

发表回复

登录后才能评论