Java 在PPT中插入OLE對象


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—

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM