PPT幻灯片中支持将文档作为OLE对象插入到PPT幻灯片指定位置,在幻灯片中可直接点击该对象,打开或编辑等。下面以插入Excel工作簿文档为例,介绍如何来插入到幻灯片。
程序运行环境
编译环境:IDEA(jdk 1.8.0)
测试文档:Excel 2013(.xlsx)、Power Point(.pptx)
Office Jar包:Free Spire.Office for Java 3.9.0
关于如何导入jar,参考如下步骤:
导入效果:
Java示例代码
import com.spire.presentation.FileFormat; import com.spire.presentation.drawing.IImageData; import com.spire.xls.*; import com.spire.presentation.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; public class AddOLE { public static void main(String[] args) throws Exception { //新建PowerPoint文档 Presentation ppt = new Presentation(); //加载Excel文档,将指定单元格数据保存为图片 Workbook workbook = new Workbook(); workbook.loadFromFile("test.xlsx"); Worksheet sheet = workbook.getWorksheets().get(0); sheet.saveToImage("image.png",1,1,8,7);//将指定单元格数据范围保存为图片 //加载保存的表格图片(作为OLE对象图标) File file = new File("image.png"); BufferedImage image = ImageIO.read(file); IImageData oleImage = ppt.getImages().append(image); Rectangle rec = new Rectangle(120, 30, image.getWidth()/2, image.getHeight()/2); //将Excel数据保存到流 File oldFile = new File("test.xlsx"); FileInputStream inputStream = new FileInputStream(oldFile); byte[] data = new byte[(int)oldFile.length()]; inputStream.read(data,0,data.length); //将Excel文档作为OLE对象插入到幻灯片 IOleObject oleObject = ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rec); oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage); oleObject.setProgId("Excel.Sheet.12"); //保存文档 ppt.saveToFile("EmbedExcelAsOLE.pptx", FileFormat.PPTX_2013); inputStream.close(); } }
OLE对象添加结果:
—End—