1. 程式人生 > WINDOWS開發 >C# 新增OLE到PPT幻燈片

C# 新增OLE到PPT幻燈片

本文介紹通過C#程式程式碼來新增OLE物件到PPT幻燈片的方法。這裡以將Excel文件為物件插入到PPT幻燈片中的指定位置;新增時,將Excel中的單元格範圍儲存為圖片,將圖片以嵌入的方式新增到幻燈片,新增成功後,可通過雙擊圖片來編輯、開啟等動作對Excel源文件進行操作。

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

獲取及新增引用:通過官網下載包。下載後,解壓安裝到指定路徑。完成安裝後,將安裝路徑下Bin資料夾中的Spire.XLS.dll和Spire.Presentation.dll新增引用到VS程式。如下引用效果:

技術分享圖片

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新增效果:

技術分享圖片