java poi ppt 介面的基本操作
阿新 • • 發佈:2020-01-06
依賴
在 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(); } }
&n