PPT中插入 OLE 对象
1、下载Spire.Office 3.9.1, 并将其中的Spire.XLS.dll文件和Spire.Presentation.dll文件引用到项目中。
2、 将代码放入Visual Studio中:
【C#】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spire.Xls;
using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Drawing;
using System.IO;
namespace Insert_OLE_to_PPT
{
class Program
{
static void Main(string[] args)
{
//加载Excel文档
Workbook book = new Workbook();
book.LoadFromFile("Sample.xlsx");
//选择单元格范围并将其保存为图像
Image image = book.Worksheets[0].ToImage(1, 1, 5, 5); //新建一个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);
}
}
}
3、调试并运行代码后,生成的文档如下图所示:
