为了在PowerPoint幻灯片放映中显示Excel数据,我们可以将Excel作为工作表对象插入到Powerpoint,本文我们将讨论如何通过Spire将Excel工作表作为OLE对象插入到幻灯片。为实现此功能,我们需要使用Spire.XLS加载Excel文件并获取数据,然后使用Spire.Presentation创建OLE对象, 所以请下载Spire.Office并引用相应的dll到应用程序。
C#
//加载Excel文档
Workbook book = new Workbook();
book.LoadFromFile(@"C:\Users\Administrator\Desktop\Book1.xlsx");
//选择单元格范围并将其保存为图像
Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);
//新建一个PowerPoint文档
Presentation ppt = new Presentation();
//插入图像到PowerPoint文档
IImageData oleImage = ppt.Images.Append(image);
Rectangle rec = new Rectangle(60, 60, image.Width, image.Height);
using (MemoryStream ms = new MemoryStream())
{
//将Excel数据保存到流
book.SaveToStream(ms);
ms.Position = 0;
//根据Excel数据将OLE对象插入到PowerPoint文档
Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);
oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;
oleObject.ProgId = "Excel.Sheet.12";
}
//保存文档
ppt.SaveToFile("插入OLE.pptx", Spire.Presentation.FileFormat.Pptx2013);
VB.NET
'加载Excel文档
Dim book As New Workbook()
book.LoadFromFile("C:\Users\Administrator\Desktop\Book1.xlsx")
'选择单元格范围并将其保存为图像
Dim image As Image = book.Worksheets(0).ToImage(1, 1, 5, 4)
'新建一个PowerPoint文档
Dim ppt As New Presentation()
'插入图像到PowerPoint文档
Dim oleImage As IImageData = ppt.Images.Append(image)
Dim rec As New Rectangle(60, 60, image.Width, image.Height)
Using ms As New MemoryStream()
'将Excel数据保存到流
book.SaveToStream(ms)
ms.Position = 0
'根据Excel数据将OLE对象插入到PowerPoint文档
Dim oleObject As Spire.Presentation.IOleObject = ppt.Slides(0).Shapes.AppendOleObject("excel", ms.ToArray(), rec)
oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
oleObject.ProgId = "Excel.Sheet.12"
End Using
'保存文档
ppt.SaveToFile("插入OLE.pptx", Spire.Presentation.FileFormat.Pptx2013)
截图展示: