This tutorial will demonstrate how to insert an Excel file as an OEL object into a PowerPoint document using Java codes. It’s worth mentioning that I used a third-party library called Free Spire.Presentation for Java to accomplish the function above.
Dependency
Before running codes, you need to
add Spire.Presentation.jar in the library to your project. You can directly download
it from the link, or reference it by using the following Maven configuration.
<repositories>
<repository>
<id>com.e-iceblue</id>
<url>http://repo.e-iceblue.cn/repository/maven-public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.presentation.free</artifactId>
<version>3.9.0</version>
</dependency>
</dependencies>
Using the code
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 InsertOEL {
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\\Test1\\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\\Test1\\Desktop\\Sample.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("output/InsertOle.pptx", FileFormat.PPTX_2013);
}
}Output
No comments:
Post a Comment