POI之PPT導出最簡單實例

開心一笑

有個四歲的小鬼問我:哥哥你怎么長得那么丑啊?
我想了想,到他耳邊說:你不要告訴別人哦。其實我就是未來的你。
他哭了。。。

提出問題

如何實現(xiàn)一個最簡單的PPT導出功能呢???

解決問題

QQ10.png

一步一步地,沒打算寫太多,感覺寫的篇幅過長,大都沒什么耐心看下去,所以每次都只解決一個小問題,開始吧......

下面是對POI的各種文件做一些簡單介紹:

  • Excel 文件: xls 格式文件對應 POI API 為 HSSF 。 xlsx 格式為 office 2007 的文件格式,POI 中對應的API 為XSSF
  • Word 文件:doc 格式文件對應的 POI API 為 HWPF。 docx 格式為 XWPF
  • powerPoint 文件:ppt 格式對應的 POI API 為 HSLF。 pptx 格式為 XSLF
  • outlook :對應的 API 為 HSMF
  • Visio: 對應的 API 為 HDGF
  • Publisher : 對應的 API 為 HPBF

下面是來自POI文檔里對類的介紹:

HSLFSlideShow:

This class contains the main functionality for the Powerpoint file "reader".(該類包含PTT讀的主要功能)

SlideShow:

This class is a friendly wrapper on top of the more scary HSLFSlideShow. TODO: - figure out how to match notes to their correct sheet (will involve understanding DocSlideList and DocNotesList) - handle Slide creation cleaner
(可以把該類理解為Slide管理類)

Slide
This class represents a slide in a PowerPoint Document. It allows access to the text within, and the layout. For now, it only does the text side of things though(該類代表PPT里的一頁幻燈片,Slide英文意思為幻燈片)

TextBox:

Represents a TextFrame shape in PowerPoint.(在PPT里代表一個文本框)

Contains the text in a text frame as well as the properties and methods that control alignment and anchoring of the text.

例一:最簡單的PPT生成實例,是針對.ppt格式做介紹,下一章,我們用.pptx做介紹

package com.hwy.test;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.SlideShow; 
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;

/**
 * PPT簡單導出
 * Created by Ay on 2016/6/14.
 */
public class MyFirstPPTTest {


    public static void main(String[] args) throws Exception{
        /** PPT文件路徑 **/
        String filePath = "D://MyPPT.ppt";
        /** 加載PPT **/
        HSLFSlideShow ppt = new HSLFSlideShow(filePath);
        /** 創(chuàng)建一個slideShow,可以理解為管理Slide的列表 **/
        SlideShow slideShow = new SlideShow(ppt);
        /** 可以 理解為PPT里的每一頁 **/
        Slide slide = slideShow.createSlide();
        /** 創(chuàng)建一個文本框 **/
        TextBox textBox = new TextBox();
        /** 設置文本框的值 **/
        textBox.setText("Hello PPT ....");
        /** 設置文本框的位置,參數(shù)分別為想x,y,width,height **/
        textBox.setAnchor(new Rectangle(10,10,100,100));
        slide.addShape(textBox);
        /** 輸出文件 **/
        slideShow.write(new FileOutputStream(filePath));
    }

}

結果:


這里寫圖片描述

讀書分享

來自《歲月的童話》

  • 回憶是一種很奇妙的東西,它生活在過去,存在于現(xiàn)在,卻能影響未來。
  • 如果真的有一天。某個回不來的人消失了。某個離不開的人離開了。也沒關系。時間會帶你去最正確的人身邊。請你先好好愛著自己。然后那個還不知道在哪里的人,會來接你。
  • 歲月似一掬清水,無論平攤還是緊握在手掌,總會有一點一滴從指縫中流逝。
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容