java poi 操作ppt

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


}

 

猜你喜欢

转载自www.cnblogs.com/dw3306/p/9974920.html