本文我们将介绍如何使用Spire.Presentation for java将Excel工作表作为OLE对象插入到幻灯片。
import com.spire.presentation.*;
import com.spire.presentation.drawing.IImageData;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class embedExcelAsOLE {
public static void main(String[] args) throws Exception{
//新建PowerPoint文档
Presentation ppt = new Presentation();
//插入图片到PowerPoint文档
File file =new File("Excel.png");
BufferedImage image = ImageIO.read(file);
IImageData oleImage = ppt.getImages().append(image);
Rectangle rec = new Rectangle(80, 60, image.getWidth(), image.getHeight());
//加载Excel文档
String input = "Sample.xlsx";
File oldFile=new File(input);
//将Excel数据保存到流
FileInputStream inputStream = new FileInputStream(oldFile);
byte[] data = new byte[(int)oldFile.length()];
inputStream.read(data,0,data.length);
//根据Excel数据将OLE对象插入到PowerPoint文档
com.spire.presentation.IOleObject oleObject=ppt.getSlides().get(0).getShapes().appendOleObject("excel", data, rec);
oleObject.getSubstituteImagePictureFillFormat().getPicture().setEmbedImage(oleImage);
oleObject.setProgId("Excel.Sheet.12");
//保存文档
ppt.saveToFile("output/embedExcelAsOLE.pptx", FileFormat.PPTX_2013);
inputStream.close();
}
}
效果图: