Embedding Excel Object into PPTX slide

Hi there, Is there an example for embedding Excel Object into PPTX slide?

I'm getting an error (please refer to the error message in attached .jpg file) when embedding Excel Object into PowerPoint SlideEx (2007) and I'm using the following code:

'Instantiate a Presentation object that represents a PPTX file (already exist)

Dim pres As PresentationEx = New PresentationEx("C:\Work\template.pptx")

'** Clone the last slide

pres.Slides.AddClone(pres.Slides(pres.Slides.Count - 1))

'*** do stuff

'Create a workbook

Dim wb As New Aspose.Cells.Workbook

' template excel file with only one chart in the sheet named "ChartSheet"

wb.Open(filePath & "\template.xlsx", FileFormatType.Excel2007Xlsx)

' get image of the chart

Dim imgChart As Bitmap = wb.Worksheets("ChartSheet").Charts(0).ToImage()

'Save the workbook to stream

Dim wbStream As MemoryStream = wb.SaveToStream

Dim imgChartEx As ImageEx = pres.Images.AddImage(imgChart)

Dim sldSize As SlideSizeEx = pres.SlideSize

Dim chartOleData(wbStream.Length) As Byte

wbStream.Position = 0

wbStream.Read(chartOleData, 0, chartOleData.Length)

Dim oof As Aspose.Slides.Pptx.OleObjectFrameEx = sld.Shapes.AddOleObjectFrame(0, 0, sldSize.Size.Width , sldSize.Size.Height, "Excel.Sheet.8", chartOleData)

oof.Image = imgChartEx

Am i doing something wrong?

Thanks

Hello,

Could you please provide template.pptx file for investigation. It looks like there is a problem with presentation file parsing.

Sure...But the error occurs even if it's newly created pptx file... The attached ppts file has nothing in it but error still occurs... I've also provided the excel template.xlsx file that I'm embedding the chart object from.

Sheet name "Template" should be "ChartSheet" in the template.xlsx.. but i don't think the problem is with the excel chart...

Many thanks!

Do you use latest Aspose.Slides version?

I don’t have any problems with opening this presentation. Ole object also added to a slide. The only problem is I can’t edit it later in PowerPoint because of “OLE Server not found” error. We are working on this problem.

There is code I used:

FileStream fis = new FileStream(“template.pptx”, FileMode.Open, FileAccess.Read);
PresentationEx pres = new PresentationEx(fis);
fis.Close();

Stream pic = new FileStream(“test.jpg”, FileMode.Open);
Bitmap rim = new Bitmap(pic, false);
ImageEx image = pres.Images.AddImage(rim);

SlideSizeEx sldSize = pres.SlideSize;
SlideEx sld = pres.Slides[0];

FileStream fs = new FileStream(“template.xlsx”, FileMode.Open, FileAccess.Read);
MemoryStream mstream = new MemoryStream();
byte[] buf = new byte[4096];

while (true)
{
int bytesRead = fs.Read(buf, 0, buf.Length);
if (bytesRead <= 0)
break;
mstream.Write(buf, 0, bytesRead);
}

OleObjectFrameEx oof = sld.Shapes.AddOleObjectFrame(0, 0, sldSize.Size.Width, sldSize.Size.Height, “Excel.Sheet.12”, mstream.ToArray());
oof.Image = image;

pres.Write(“template_.pptx”);

Hi there,

Thank you for your reply. The problem lies in the cloning part. Please try to incorporate in your code, cloning of the slide and embedding another excel object (i.e. from the same template.xlsx file) in the cloned slide. That is where the issue lies for me. I can add an Excel object in the first slide (just like your example) but not in the cloned slide (please refer to my example).

p.s. I'm programming in VB .NET. Not sure if that makes a difference. And I'm using Aspose Slides.dll 3.11

Thanks

Hi Twin,

Please download the Aspose.Slides for .NET 4.0.0 here and use the following VB.NET code that works fine for your requirements.

Dim fis As FileStream = New FileStream("d:\ppt\twin\template.pptx", FileMode.Open, FileAccess.Read)

Dim pres As PresentationEx = New PresentationEx(fis)

pres.Slides.AddClone(pres.Slides(pres.Slides.Count - 1))

fis.Close()

Dim pic As Stream = New FileStream("d:\ppt\twin\waterfall.jpg", FileMode.Open)

Dim rim As Bitmap = New Bitmap(pic, False)

Dim image As ImageEx = pres.Images.AddImage(rim)

Dim sldSize As SlideSizeEx = pres.SlideSize

Dim sld As SlideEx = pres.Slides(0)

Dim fs As FileStream = New FileStream("d:\ppt\twin\template.xlsx", FileMode.Open, FileAccess.Read)

Dim mstream As MemoryStream = New MemoryStream()

Dim buf(4096) As Byte '= New Byte(4096)

While (True)

Dim bytesRead As Integer = fs.Read(buf, 0, buf.Length)

If bytesRead <= 0 Then

Exit While

End If

mstream.Write(buf, 0, bytesRead)

End While

Dim oof As OleObjectFrameEx = sld.Shapes.AddOleObjectFrame(0, 0, sldSize.Size.Width, sldSize.Size.Height, "Excel.Sheet.12", mstream.ToArray())

oof.Image = image

pres.Write("d:\ppt\twin\template_.pptx")