java poi 操作ppt
阿新 • • 發佈:2018-11-17
java poi 操作ppt
可以參考:
https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html
http://blog.sina.com.cn/s/blog_657d630f0100nltw.html
http://blog.csdn.net/zt_fucker/article/details/52290028
http://blog.csdn.net/mike_caoyong/article/details/28651665
http://blog.csdn.net/littlebeat123/article/details/46483323
http://bbs.csdn.net/topics/380182913
http://blog.csdn.net/zhongweijian/article/details/8299531
http://www.anyrt.com/blog/list/poippt.html
具體:
1:maven 依賴:
<!--匯出檔案--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>
java程式碼:
package com.dengwei.day01springboot.utils; import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.util.IOUtils; import org.apache.poi.xslf.usermodel.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File;import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import static org.apache.poi.xslf.usermodel.SlideLayout.TITLE_AND_CONTENT; /** * @Author * @ClassName AddImgToPPt * @Description TODO * @Date 2018/11/17 0017 下午 3:28 * @Version 1.0 */ public class AddImgToPPt { public static void main(String args[]) throws IOException { // 建立ppt: XMLSlideShow ppt = new XMLSlideShow(); //設定幻燈片的大小: Dimension pageSize = ppt.getPageSize(); pageSize.setSize(800,700); //獲取幻燈片主題列表: List<XSLFSlideMaster> slideMasters = ppt.getSlideMasters(); //獲取幻燈片的佈局樣式 XSLFSlideLayout layout = slideMasters.get(0).getLayout(SlideLayout.TITLE_AND_CONTENT); //通過佈局樣式建立幻燈片 XSLFSlide slide = ppt.createSlide(layout); // 建立一張無樣式的幻燈片 // XSLFSlide slide = ppt.createSlide(); //通過當前幻燈片的佈局找到第一個空白區: XSLFTextShape placeholder = slide.getPlaceholder(0); XSLFTextRun title = placeholder.setText("成都智互聯科技有限公司"); XSLFTextShape content = slide.getPlaceholder(1); // 投影片中現有的文字 content.clearText(); content.setText("圖片區"); // reading an image File image = new File("F:\\workroom\\img\\class2.jpg"); //獲取圖片資訊: BufferedImage img = ImageIO.read(image); // converting it into a byte array byte[] picture = IOUtils.toByteArray(new FileInputStream(image)); // adding the image to the presentation XSLFPictureData idx = ppt.addPicture(picture, PictureData.PictureType.PNG); // creating a slide with given picture on it XSLFPictureShape pic = slide.createPicture(idx); //設定當前圖片在ppt中的位置,以及圖片的寬高 pic.setAnchor(new java.awt.Rectangle(360, 200, img.getWidth(), img.getHeight())); // creating a file object File file = new File("F:\\workroom\\img\\AddImageToPPT.pptx"); FileOutputStream out = new FileOutputStream(file); // saving the changes to a file ppt.write(out); System.out.println("image added successfully"); out.close(); } }