How to add excel sheet into ppt using java?

Hi Admin,

I want to add a excel sheet into a ppt ,for that i have written the following code,but i am not getting the output.Could you please look into this issue ?

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class PPTTest1 {

public  void setAsposeLicense() {
	InputStream inputStream = null;
	try {
		License license = new License();
		inputStream = getClass().getClassLoader().getResourceAsStream("Aspose_Total.lic");
		license.setLicense(inputStream);
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (inputStream != null)
				inputStream.close();
		} catch (Exception e) {
			inputStream = null;
		}
	}
}
public static void main(String[] args) throws IOException {
		PPTTest1 ppt=new PPTTest1();
		ppt.setAsposeLicense();
		//Instantiate Prseetation class that represents the PPTX
		Presentation pres = new Presentation();
		//Access the first slide
		ISlide sld = pres.getSlides().get_Item(0);
		//Load an Excel file to Array of Bytes
		File file=new File("E:\\R&D\\powerBI.xlsx");
		int length=(int)file.length();
		FileInputStream fstro = new FileInputStream(file);
		byte[] buf = new byte[length];
		fstro.read(buf, 0, length);
		//Add an Ole Object Frame shape
		IOleObjectFrame oof = sld.getShapes().addOleObjectFrame((float)0,(float) 0, (float)pres.getSlideSize().getSize().getWidth(),(float) pres.getSlideSize().getSize().getHeight (), "Excel.Sheet.8", buf);
		//Write the PPTX to disk
		pres.save("E:\\R&D\\Aspose\\OleEmbed.pptx", SaveFormat.Pptx);
		System.out.println("ppt generated.........");

	}
	 		
}

@manoranjan,

I have observed the sample code shared by you. You are using XLSX file in your application where as you have used “Excel.Sheet.8” as class name when adding Ole frame. You need to use “Excel.Sheet.12” as class name when working with XLSX file. The former is valid for XLS file formats only.

Hi Mudassir,

The above solution is not working for me.Could you please check for other ?

Regards,
Manoranjan

Hi Mudassir,

Please find attached video file which contains the exact requirement what we need.
Please send sample java code according to that.add_excel_to_ppt.zip (1.4 MB)

Regards,
Manoranjan

@manoranjan,

I have observed the video shared by you and like to share that you are trying to add an Excel workbook OLE frame inside presentation. As requested earlier, please provide the excel file that you want to add as OLE frame. If you want to generate the Excel file on runtime using Aspose.Cells and then adding that as OLE frame inside presentation, then in that case, I suggest you to please visit this documentation link for your convenience. You may also check, Working with OLE frames documentation section as well.

Hi Mudassir,

Thank you for your reply.powerBI.zip (9.5 KB)

Please find the attached excel file which i want to add into ppt.

Regards,
Manoranjan

Hi Mudassir,

I gone through your link & now i am able to add the excel into ppt dynamically.
but when i am opening the output which is in attachment its not showing the excel directly .if i am double clicking on that then its showing the excel on ppt .

Please find the output in the attachment & below code

i want see the excel directly when i will open the ppt. Could please help on this ?

Thanks in advance.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import com.aspose.cells.Cell;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.slides.IOleObjectFrame;
import com.aspose.slides.ISlide;
import com.aspose.slides.License;
import com.aspose.slides.Presentation;
import com.aspose.slides.SaveFormat;

public class ExcelTest {
public void setAsposeLicense() {
InputStream inputStream = null;
try {
License license = new License();
inputStream = getClass().getClassLoader().getResourceAsStream(
“Aspose_Total.lic”);
license.setLicense(inputStream);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception e) {
inputStream = null;
}
}
}

public static void main(String[] args) throws Exception {
	ExcelTest et = new ExcelTest();
	et.setAsposeLicense();
	et.createExcel();
}

private void createExcel() throws Exception {
        //Create a workbook
	    Workbook wb = new Workbook();
	    //Add an excel chart
	    int chartRows = 25;
	    int chartCols = 15;
	    Worksheet dataSheet =wb.getWorksheets().get(0);
	    Cell cell = dataSheet.getCells().get("A1");
	    // Input the "Hello World!" text into the "A1" cell
	    cell.setValue("Hello World!");
	    String sheetName = "DataSheet";
	    dataSheet.setName(sheetName);
	   //Set chart ole size
	   wb.getWorksheets().setOleSize(0, chartRows, 0, chartCols);
	   //Save the workbook to stream
	   ByteArrayOutputStream bout=new ByteArrayOutputStream();		 
	   wb.save(bout,com.aspose.cells.SaveFormat.EXCEL_97_TO_2003);
	   //Create a presentation
	   Presentation pres = new Presentation();
	   ISlide sld = pres.getSlides().get_Item(0);
	   //Add the workbook on slide
	   AddExcelChartInPresentation(pres, sld, bout.toByteArray());
	   //Write the presentation to disk
	   pres.save("E:\\R&D\\Aspose\\outputJ.pptx", SaveFormat.Pptx);
       System.out.println("ppt generated....");

}

static void AddExcelChartInPresentation(Presentation pres, ISlide sld, byte[] wbArray) throws Exception
{
    double oleHeight = pres.getSlideSize().getSize().getHeight();
    double oleWidth = pres.getSlideSize().getSize().getWidth();
    Workbook wb=new Workbook();	 
    //Creating and EXCEL_97_TO_2003 LoadOptions object
    com.aspose.cells.LoadOptions loadOptions = new com.aspose.cells.LoadOptions(com.aspose.cells.FileFormatType.EXCEL_97_TO_2003);         
    Workbook wb1=new Workbook(new ByteArrayInputStream(wbArray),loadOptions);	 
    IOleObjectFrame oof = sld.getShapes().addOleObjectFrame(0f, 0f, (float)oleWidth, (float)oleHeight, "Excel.Sheet.8", wbArray);
 
}	

}
outputJ.zip (29.4 KB)

Regards,
Manoranjan

@manoranjan,

I have observed the output shared by you and like to share that it is not an issue with Aspose.Slides but implementation behavior of OLE engine itself. By default, when OLE is added it shows red, “Embedded OLE” message for OLE frame to show and OLE frame in disabled form. When you double click on OLE frame, it gets activated and you get preview of OLE data. There are two possible solutions for this. First is to involve Aspose.Cells in your implementation and generate the worksheet image and set that as OLE frame image in disabled form to avoid red message. The other option is to use third party macros in your presentation whose role is to activate the OLE frame as soon as presentation gets opened in PowerPoint. I suggest you to please visit documentation link, Updating OLE objects automatically for your kind reference.

Hi Mudassir,

Please find the attached output.outputJ.zip (29.2 KB)

After double clicking on ppt when i am trying to see excel in slide show view , its not coming properly.
Could you please check the issue once ?

Regards,
Manoranjan

@manoranjan,

I have observed your comments related to resizing of OLE frame in generated presentation. An issue with ID SLIDESJAVA-36633 has been added in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be automatically notified once the issue will be fixed.

Hi Mudassir,

How can i adjust excel height & width as same as ppt i.e. excel should fit with ppt ?

Regards,
Manoranjan

@manoranjan,

I like to share that for Aspose.Cells related inquiries you may please contact Aspose.Cells forum. For fitting excel sheet inside slide and avoiding it to get resized when double clicking OLE frame, we have already added an issue for investigation and resolution.

The issues you have found earlier (filed as SLIDESJAVA-36633) have been fixed in Aspose.Slides for Java 17.12 . Please try using the latest release version and in case you experience any issue or you have any further query, please feel free to contact.