Embed an Excel File in a PowerPoint Document in Java

2020-12-01 07:51:32 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to insert an Excel file as an OEL object into a PowerPoint document using Spire.Presentation for Java.

import com.spire.presentation.FileFormat;
import com.spire.presentation.IOleObject;
import com.spire.presentation.Presentation;
import com.spire.presentation.SlideSizeType;
import com.spire.presentation.drawing.IImageData;
import javax.imageio.ImageIO;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;

public class EmbedExcel {

    public static void main(String[] args) throws Exception {

        //Create a Presentation object
        Presentation ppt = new Presentation();
        ppt.getSlideSize().setType(SlideSizeType.SCREEN_16_X_9);

        //Load an image file and add it to the image collection of the presentation
        File file = new File("C:\\Users\\Administrator\\Desktop\\image.png");
        BufferedImage image = ImageIO.read(file);
        IImageData oleImage = ppt.getImages().append(image);

        //Load an Excel file and convert it to byte[] object
        String excelPath = "C:\\Users\\Administrator\\Desktop\\data.xlsx";
        File excelFile = new File(excelPath);
        FileInputStream inputStream = new FileInputStream(excelFile);
        byte[] data = new byte[(int) excelFile.length()];
        inputStream.read(data, 0, data.length);

        //Create a Rectangle2D object
        Rectangle2D rect = new Rectangle2D.Float(60, 60, image.getWidth(), image.getHeight());

        //Insert the Excel file as an OLE object to the first slide
        IOleObject oleObject = ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rect);
        oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
        oleObject.setProgId("Excel.Sheet.12");

        //Save to another file
        ppt.saveToFile("InsertOle.pptx", FileFormat.PPTX_2013);
    }
}

Embed an Excel File in a PowerPoint Document in Java

Additional Info

  • tutorial_title:
Last modified on Thursday, 02 September 2021 06:11