依賴
在 pom.xml中增加以下依賴
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency>
注:很多博客,教我們用以下依賴,是沒有XSSF相關內容的
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency>
version 版本
poi的版本可以在 https://mvnrepository.com/artifact/org.apache.poi/poi 進行查詢。
找到想要依賴的版本
點擊進入后,可以直接復制里面的依賴
初始化
import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; SlideShow slideShow = SlideShowFactory.create(new File("./res/1.pptx"));
- 如果文件不存在或文件正在使用,create 方法拋出 IOException 異常
- 如果文件損壞,create 方法拋出 EncryptedDocumentException 異常
- 返回值 SlideShow 代表整個ppt文檔,可以通過 SlieShow.getSlides() 獲取每張幻燈片進行操作,以及獲取整個ppt的信息,如頁面大小(getPageSize, setPageSize),圖片數據(getPictureData, setPictureData),字體,輸出到流等
- 通過SlideShow.getSlides()后操作Slide都可以反應到SlideShow。
Slide
- slide代表幻燈片的一頁
- 也是代表open-xml中 /ppt/sliders/*.xml 中的一個xml文檔
- Slide只有一個實現類 XSLFSlide
獲取方法
XSLFSlide未提供public的構造函數,獲取只能通過SlideShow中提供的createSlide, getSlides方法獲取。
如圖:XSLFSlide的構造函數,都是私有的
如圖:SlideShow中提供的Slide獲取方法, 由於 Slide 接口只有一個實現(XLSFSlide),所以可以直接轉換成實現類(XLSFSlide)操作
常用操作
-
獲取所有的備注
XSLFNotes notes = slider.getNotes();
-
獲取所有的批注
List<XSLFComment> comments = slider.getComments();
- 獲取所有的關聯部分,包括:備注,批注,圖片,圖表,母版等
List<POIXMLDocumentPart.RelationPart> relationParts = slider.getRelationParts();
-
- 獲取備注,批注都是從 getRelationParts中獲取的
- 除了圖形的獲取,其他元素的獲取都可以通過此方法獲取(通過遍歷判斷類型)
- 獲取所有的圖形
List<XSLFShape> shapes = slider.getShapes();
POIXMLDocumentPart
POIXMLDocmentPart是一個ppt關聯的部分的具體內容,包括:備注、批注、圖片、圖表、母版等。
通過 POIXMLDocumentPart documentPart = POIXMLDocumentPart.RelationPart.getDocumentPart() 獲取。
- 批注部分, 與 slider.getComments() 對應
if (documentPart instanceof XSLFComments) { XSLFComments comments1 = (XSLFComments) documentPart; }
-
圖表部分,有多個,每個圖表一個 XSLFChart
if (documentPart instanceof XSLFChart) { XSLFChart chart = (XSLFChart) documentPart; }
-
備注部分, 與 slider.getNotes() 相同
if (documentPart instanceof XSLFNotes) { XSLFNotes notes1 = (XSLFNotes) documentPart; }
- 獲取備注的一行文本
// 文本段落,一行為一段 List<List<XSLFTextParagraph>> textParagraphs = notes1.getTextParagraphs(); // 第一行的所有文本,包含文本樣式 List<XSLFTextRun> textRuns = textParagraphs.get(0).get(0).getTextRuns(); // 第一行的文本內容 String text = textParagraphs.get(0).get(0).getText();
-
母版,只有一個
if (documentPart instanceof XSLFSlideLayout) { XSLFSlideLayout relation1 = (XSLFSlideLayout) documentPart; }
XSLFShape
獲取 shape 的文本
for (XSLFShape shape : shapes) { if (shape instanceof XSLFTextShape) { // 有文本的 sharpe XSLFTextShape textShape = (XSLFTextShape) shape; // 文本段落,一行為一段 List<XSLFTextParagraph> textParagraphs = textShape.getTextParagraphs(); // 第一行的所有文本,包含文本樣式 List<XSLFTextRun> textRuns = textParagraphs.get(0).getTextRuns(); // 第一行的文本內容 String text = textParagraphs.get(0).getText(); } else if (shape instanceof XSLFGroupShape) { // 圖形組合 XSLFGroupShape groupShape = (XSLFGroupShape) shape; // 圖形組合下的圖形,可以與 slider.getShapes() 獲取的list一樣操作 List<XSLFShape> groupShapeShapes = groupShape.getShapes(); } }