原創(chuàng)|使用教程|編輯:王香|2017-08-04 17:05:14.000|閱讀 1332 次
概述:Spire.Presentation是一個(gè)專業(yè)的 PowerPoint控件,用于幫助開發(fā)人員高效地創(chuàng)建、讀取、編輯、轉(zhuǎn)換和打印任PPT文檔,本文介紹了如何在C#,VB.NET中將Excel對象嵌入PPT幻燈片。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
有時(shí)候我們需要在Excel幻燈片放映中呈現(xiàn)Excel數(shù)據(jù),而不是切換到Excel,為此,可以將Excel數(shù)據(jù)以其他格式(如工作表對象,HTML格式,位圖,圖片或文本格式)粘貼到PowerPoint幻燈片。 在這篇文章中,將會(huì)給大家介紹如何將Excel工作表作為一個(gè)OLE對象插入PowerPoint中的C#,VB.NET中。
首先,我們必須使用Spire.XLS 來加載Excel文件并獲取數(shù)據(jù),然后使用Spire.Presentation 創(chuàng)建OEL對象。 所以請記得下載Spire.Office,并將所提到的dll添加為VS項(xiàng)目的參考。 然后按照以下指導(dǎo)完成這項(xiàng)工作。
Step 1:創(chuàng)建一個(gè)新的Workbook實(shí)例并加載Excel文件。
Workbook book = new Workbook(); book.LoadFromFile(@"data.xlsx");
Step 2:從第一個(gè)工作表中選擇單元格范圍,并保存為圖像。
mage image = book.Worksheets[0].ToImage(1, 1, 5, 4);
Step 3:初始化Presentation類的新實(shí)例,將圖像添加到演示文稿中。
Presentation ppt = new Presentation(); IImageData oleImage = ppt.Images.Append(image);
Step 4:根據(jù)Excel數(shù)據(jù)將OLE對象插入到演示文稿中。
Rectangle rec = new Rectangle(60,60,image.Width,image.Height); using (MemoryStream ms = new MemoryStream()) { book.SaveToStream(ms); ms.Position = 0; Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec); oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage; oleObject.ProgId = "Excel.Sheet.8";
Step 5:以指定的格式保存PowerPoint文件。
ppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007);
輸出:
詳細(xì)代碼:
[C#]
Workbook book = new Workbook(); book.LoadFromFile(@"data.xlsx"); Image image = book.Worksheets[0].ToImage(1, 1, 5, 4); Presentation ppt = new Presentation(); IImageData oleImage = ppt.Images.Append(image); Rectangle rec = new Rectangle(60,60,image.Width,image.Height); using (MemoryStream ms = new MemoryStream()) { book.SaveToStream(ms); ms.Position = 0; Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec); oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage; oleObject.ProgId = "Excel.Sheet.8"; } ppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007);}
[VB.NET]
Dim book As New Workbook() book.LoadFromFile("data.xlsx") Dim image As Image = book.Worksheets(0).ToImage(1, 1, 5, 4) Dim ppt As New Presentation() Dim oleImage As IImageData = ppt.Images.Append(image) Dim rec As New Rectangle(60, 60, image.Width, image.Height) Using ms As New MemoryStream() book.SaveToStream(ms) ms.Position = 0 Dim oleObject As Spire.Presentation.IOleObject = ppt.Slides(0).Shapes.AppendOleObject("excel", ms.ToArray(), rec) oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage oleObject.ProgId = "Excel.Sheet.8"; End Using ppt.SaveToFile("InsertOle.ppt", Spire.Presentation.FileFormat.Pptx2007)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn