本文介绍通过C#程序代码来添加OLE对象到PPT幻灯片的方法。这里以将Excel文档为对象插入到PPT幻灯片中的指定位置;添加时,将Excel中的单元格范围保存为图片,将图片以嵌入的方式添加到幻灯片,添加成功后,可通过双击图片来编辑、打开等动作对Excel源文档进行操作。

使用工具:Free Spire.Office for .NET(免费版)

获取及添加引用:通过官网​下载包​。下载后,解压安装到指定路径。完成安装后,将安装路径下Bin文件夹中的Spire.XLS.dll和Spire.Presentation.dll添加引用到VS程序。如下引用效果:

C# 添加OLE到PPT幻灯片_C#

 

C# 代码

using Spire.Xls;
using Spire.Presentation;
using System.Drawing;
using Spire.Presentation.Drawing;
using System.IO;

namespace AddOLE
{
class Program
{
static void Main(string[] args)
{
//加载Excel文档
Workbook book = new Workbook();
book.LoadFromFile("WorkBook.xlsx");

//选择单元格范围并将其保存为图像
Image image = book.Worksheets[0].ToImage(1, 1, 4, 3);

//新建一个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;

//将OLE对象插入到PPT中的第1张幻灯片
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("AddOLE.pptx", Spire.Presentation.FileFormat.Pptx2013);
System.Diagnostics.Process.Start("AddOLE.pptx");
}
}
}

OLE添加效果:

C# 添加OLE到PPT幻灯片_Spire.Office_02