java利用poi生成pptx格式的ppt

最近开发了一个自定义ppt模板生成ppt的需求,记录一个小demo
官方文档 ppt文档链接
w3c的文档ppt文档
1.首先打入poi的maven依赖

        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
        <dependency>    
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>

2.代码示例


//    步骤1 :通过实例化 XMLSlideShow 类创建一个空的演示文稿,如下所示:
        XMLSlideShow ppt = new XMLSlideShow();
        String slideWidth = "25.36";
        String slideHeight = "19.01";
//      设置幻灯片大小
        ppt.setPageSize(new Dimension(Double.valueOf(Double.valueOf(slideWidth) / 3.53 * 100).intValue(), Double.valueOf(Double.valueOf(slideHeight) / 3.53 * 100).intValue()));
//    步骤2 :使用 getSlideMasters()方法获取幻灯片主题列表。 此后,使用索引选择所需的幻灯片母带,如下所示:
        XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);//可设置母版的
//    这里我们得到的默认幻灯片母版是在幻灯片主数据的第0位置。
        byte[] bt2 = FileUtils.readFileToByteArray(new File("C:\\Users\\zengjun\\Desktop\\导出ppt图片\\母版.png"));
        XSLFPictureData idx2 = ppt.addPicture(bt2, XSLFPictureData.PictureType.PNG);
        XSLFPictureShape picture = slideMaster.createPicture(idx2);//设置母版
        picture.setAnchor(new Rectangle(0, 0, ppt.getPageSize().width
                , ppt.getPageSize().height)); //设置母版大小
        //创建幻灯片
        XSLFSlide slide1 = ppt.createSlide();

        String x = "3.45";  //水平位置
        String y = "12.70"; //垂直位置
        String w = "12.45"; //宽度
        String h = "0.50";  //高度
        XSLFTextBox textBox = slide1.createTextBox();
        textBox.setAnchor(new Rectangle2D.Double(Double.valueOf(x) / 3.53 * 100, Double.valueOf(y) / 3.53 * 100, Double.valueOf(w) / 3.53 * 100, Double.valueOf(h) / 3.53 * 100));
        XSLFTextRun textRun = textBox.setText("实际发布:10");
        textRun.setFontFamily("宋体");
        textRun.setFontSize(16.0);
        textRun.setBold(true); //设置粗体状态

        String x2 = "2.96";  //水平位置
        String y2 = "13.91"; //垂直位置
        String w2 = "10.56"; //宽度
        String h2 = "0.50";  //高度

        XSLFTextBox textBox2 = slide1.createTextBox();
        textBox2.setAnchor(new Rectangle2D.Double(Double.valueOf(x2) / 3.53 * 100, Double.valueOf(y2) / 3.53 * 100, Double.valueOf(w2) / 3.53 * 100, Double.valueOf(h2) / 3.53 * 100));
        /** 生成一个新的文本段落 **/
        XSLFTextParagraph xslfTextRuns = textBox2.addNewTextParagraph();
        /** 添加新的文本 **/
        XSLFTextRun xslfTextRun = xslfTextRuns.addNewTextRun();
        /** 添加新的文本 **/
        xslfTextRun.setText("福海信息港");
        /** 设置字体类型 **/
        xslfTextRun.setFontFamily("宋体");
        /** 设置字体大小 **/
        xslfTextRun.setFontSize(16.0);

        ppt.write(new FileOutputStream("ppt2.pptx"));

3 效果展示
在这里插入图片描述

在这里插入图片描述
代码中的红色部分,是我比例算出来的大小
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_32454921/article/details/86622900