C? Add OLE to PPT slide

Keywords: C# Excel

This paper introduces the method of adding OLE objects to PPT slides through C ා program code. Here, the excel document is inserted into the specified position in the PPT slide. When adding, the cell range in excel is saved as a picture, and the picture is added to the slide in an embedded way. After adding successfully, you can double-click the picture to edit and open the Excel source document.

Using tool: free fire.office for. Net (free version)

Get and add references: through the official website Download package . After downloading, unzip and install to the specified path. When the installation is complete, add the Spire.XLS.dll and Spire.Presentation.dll in the Bin folder of the installation path to the VS program. The following reference effects:

 

C# code

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)
        {
            //Load Excel File
            Workbook book = new Workbook();
            book.LoadFromFile("WorkBook.xlsx");

            //Select a range of cells and save it as an image
            Image image = book.Worksheets[0].ToImage(1, 1, 4, 3);

            //Build a new one. PowerPoint File
            Presentation ppt = new Presentation();

            //Insert image to PowerPoint File
            IImageData oleImage = ppt.Images.Append(image);
            Rectangle rec = new Rectangle(60, 60, image.Width, image.Height);

            using (MemoryStream ms = new MemoryStream())
            {
                //take Excel Data saved to stream
                book.SaveToStream(ms);
                ms.Position = 0;

                //take OLE Objects inserting into PPT Slide 1 in
                Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);
                oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;
                oleObject.ProgId = "Excel.Sheet.12";
            }

            //Save document
            ppt.SaveToFile("AddOLE.pptx", Spire.Presentation.FileFormat.Pptx2013);
            System.Diagnostics.Process.Start("AddOLE.pptx");
        }
    }
}

OLE add effect:

Posted by fatalcure on Mon, 13 Apr 2020 11:20:45 -0700