How to Embed Excel Object into PowerPoint Slide in C#, VB.NET

To display Excel data in a PowerPoint presentation, we can copy and paste the data to a slide as a PowerPoint table, a worksheet object, a picture, or plain text. This article introduces how to insert an Excel sheet in PowerPoint as an OLE object in C# and VB.NET.

In this solution, you'll need to use Spire.XLS to load the Excel file and get the data, and then create OEL object using Spire.Presentation. So, please download Spire.Office and reference the related DLLs in your project.

Workbook book = new Workbook();
book.LoadFromFile(@"data.xlsx");

Step 2: Select the cell range from the first worksheet and save it as image.

Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);

Step 3: Initialize a new instance of Presentation class, add the image to presentation.

Presentation ppt = new Presentation();
IImageData oleImage = ppt.Images.Append(image);

Step 4: Insert an OLE object to presentation based on the Excel data.

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.12";
}

Step 5: Save the PowerPoint file with the specified format.

ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007);

Output:

How to Embed Excel Object into PowerPoint Slide in C#, VB.NET

Entire Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Xls;
using System.Drawing;
using System.IO;
namespace ExceltoPPT
{

    class Program
    {

        static void Main(string[] args)
        {
            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.12";
            }

            ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007);


        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports Spire.Xls
Imports System.Drawing
Imports System.IO
Namespace ExceltoPPT

	Class Program

		Private Shared Sub Main(args As String())
			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.12"
			End Using

			ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007)


		End Sub
	End Class
End Namespace