POI之图片导出到PPT简单实例

开心一笑

男:问你个问题,你喜欢猫还是狗?
女:狗狗。
男:汪汪汪!
女:……
男:我太幸福了,你竟然喜欢我!

视频教程

大家好,我录制的视频《Java之优雅编程之道》已经在CSDN学院发布了,有兴趣的同学可以购买观看,相信大家一定会收获到很多知识的。谢谢大家的支持……

视频地址:http://edu.csdn.net/lecturer/994

提出问题

POI如何实现图片导出到PPT中???

简书地址:http://www.jianshu.com/users/d38a3668be58/latest_articles

解决问题

package com.hwy.test;

import org.apache.poi.hslf.model.Picture;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * PPT简单导出
 * Created by Ay on 2016/6/14.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** 文件路径 **/
        String filePath = "D://MyPPT.pptx";
        String imagePath = "D://a.png";
        String imagePath2 = "D://b.png";
        String imagePath3 = "D://c.png";
        /** 加载PPT **/
        XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(filePath));

        /** 创建一个slide,理解为PPT里的每一页 **/
        XSLFSlide slide = ppt.createSlide();
        XSLFSlide slide2 = ppt.createSlide();
        XSLFSlide slide3 = ppt.createSlide();
        /** 生成二进制数组 **/
        byte[] pictureData = IOUtils.toByteArray(new FileInputStream(imagePath));
        byte[] pictureData2 = IOUtils.toByteArray(new FileInputStream(imagePath2));
        byte[] pictureData3 = IOUtils.toByteArray(new FileInputStream(imagePath3));
        /** 添加图片,返回索引 **/
        int pictureIndex =  ppt.addPicture(pictureData, XSLFPictureData.PICTURE_TYPE_PNG);
        int pictureIndex2 =  ppt.addPicture(pictureData2, XSLFPictureData.PICTURE_TYPE_PNG);
        int pictureIndex3 =  ppt.addPicture(pictureData3, XSLFPictureData.PICTURE_TYPE_PNG);
        /** 打印信息 **/
        System.out.println("pictureIndex " + pictureIndex);// pictureIndex   0
        System.out.println("pictureIndex2 " + pictureIndex2);//  pictureIndex2  1
        System.out.println("pictureIndex3 " + pictureIndex3);//  pictureIndex3  2
        /** 创建图片 **/
        XSLFPictureShape pictureShape = slide.createPicture(pictureIndex);
        XSLFPictureShape pictureShape2 = slide2.createPicture(pictureIndex2);
        XSLFPictureShape pictureShape3 = slide3.createPicture(pictureIndex3);

        /** 设置图片的位置 四个参数分别为 x y width height  **/
        pictureShape.setAnchor(new java.awt.Rectangle(50, 50, 500, 300));
        pictureShape2.setAnchor(new java.awt.Rectangle(50, 150, 500, 300));
        pictureShape3.setAnchor(new java.awt.Rectangle(50, 250, 500, 300));

        /** 获取图片类别 **/
        int pictureType = pictureShape.getPictureData().getPictureType();

        switch (pictureType){
            case Picture.JPEG:
                System.out.println("the type of picture is : " + "JPEG");
                break;
            case Picture.PNG:
                System.out.println("the type of picture is :" + "PNG");
                break;
        }

        System.out.println(pictureType);

        /** 输出文件 **/
        ppt.write(new FileOutputStream(filePath));
    }

}

读书感悟

来自《恋恋笔记本》

  • 一生至少该有一次,为了某个人而忘了自己,不求有结果,不求同行,不求曾经拥有,甚至不求你爱我。只求在我最美的年华里,遇到你。
    In your life, there will at least one time that you forget yourself for someone, asking for no result, no company, no ownership nor love. Just ask for meeting you in my most beautiful years.

  • 爱情没有那么多借口,如果最终没能在一起,只能说明爱的不够。

  • 我 没有什么特别的 只是一个 有着普通想法的普通人 过着普通的生活 没有人为我立碑著书 歌功颂德 我的名字也将很快被人遗忘 但是在一件事情上 我比任何人都要成功 我全心全意地爱着一个人 而且对我来说 这就足够了。

其他

如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿_毅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值