Java 在PPT中插入OLE对象

2025-10-04 19:06:45

1、按照如下步骤导入jar文件:

Java 在PPT中插入OLE对象

Java 在PPT中插入OLE对象

Java 在PPT中插入OLE对象

2、导入结果:

Java 在PPT中插入OLE对象

3、完成导入后,在程序中键入如下代码:

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();
   }
}

4、执行程序,生成结果文档:

Java 在PPT中插入OLE对象

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢