Posted on
Embed Excel in PowerPoint as OLE Object in Java
If you want to distribute a PowerPoint presentation that contains an Excel spreadsheet to your colleagues or customers, embedding the Excel data to the presentation is the best choice, because readers can easily open and view, or even make changes to the Excel data with no need to have access to the original Excel document.
In this article, I am going to introduce how to embed Excel in PowerPoint as OLE object by using Spire.Office for Java.
Installing Spire.Office.jar
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file fromthis link and add it as a dependency in your application.
<repositories><repository><id>com.e-iceblue</id><name>e-iceblue</name><url>http://repo.e-iceblue.com/nexus/content/groups/public/</url></repository></repositories><dependencies><dependency><groupId>e-iceblue</groupId><artifactId>spire.office</artifactId><version>4.2.0</version></dependency></dependencies>
Main steps involved
Step 1. Create an instance of Workbook class which is under the com.spire.xls.Workbook namespace, to load an Excel document. Call saveToImage(int firstRow, int firstColumn, int lastRow, int lastColumn) method to convert the selected cell range to an image. The image will be inserted to PowerPoint and displayed as an icon for the Excel to be embedded. You can also specify any other image as the icon.
Step 2. Create a Presentation object. You can use it either to create a new PowerPoint document or to load an existing PowerPoint file. And then, append the image to ImageCollection of the presentation.
Step 3. Convert the Excel document to byte[], which is one of the key parameters for creating OLE object.
Step 4. Append OLE object to slide, then specify the OLE object type and display image.
Using the code
importcom.spire.presentation.FileFormat;importcom.spire.presentation.IOleObject;importcom.spire.presentation.Presentation;importcom.spire.presentation.SlideSizeType;importcom.spire.presentation.drawing.IImageData;importcom.spire.xls.Workbook;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileInputStream;publicclassEmbedExcel{publicstaticvoidmain(String[]args)throwsException{//Specify excel pathStringexcelPath="C:\\Users\\Administrator\\Desktop\\Product.xlsx";//Create a Workbook object to load excel fileWorkbookwb=newWorkbook();wb.loadFromFile(excelPath);//Set the margins to 0 so that only the selected cell range data will display in slidewb.getWorksheets().get(0).getPageSetup().setBottomMargin(0);wb.getWorksheets().get(0).getPageSetup().setTopMargin(0);wb.getWorksheets().get(0).getPageSetup().setLeftMargin(0);wb.getWorksheets().get(0).getPageSetup().setRightMargin(0);//Convert the selected range to imageBufferedImageimage=wb.getWorksheets().get(0).saveToImage(1,1,5,5);//Create a Presentation objectPresentationppt=newPresentation();ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);//Append image to pptIImageDataoleImage=ppt.getImages().append(image);//Load the excel file and convert it to byte[] objectFileexcelFile=newFile(excelPath);FileInputStreaminputStream=newFileInputStream(excelFile);byte[]data=newbyte[(int)excelFile.length()];inputStream.read(data,0,data.length);//Create a Rectangle2D objectRectangle2Drect=newRectangle2D.Float(60,60,image.getWidth(),image.getHeight());//Insert the Excel file as an OLE object to the first slideIOleObjectoleObject=ppt.getSlides().get(0).getShapes().appendOleObject("excel",data,rect);oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);oleObject.setProgId("Excel.Sheet.12");//Save to another fileppt.saveToFile("InsertOle.pptx",FileFormat.PPTX_2013);}}
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse