本文介紹通過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添加效果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/52822.html
標籤:C#
上一篇:Navicat 密碼加密演算法
下一篇:.NET專案升級:可為空參考
