在 pom.xml中增长如下依赖apache
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.1</version> </dependency>
注:不少博客,教咱们用如下依赖,是没有XSSF相关内容的ide
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency>
poi的版本能够在 https://mvnrepository.com/artifact/org.apache.poi/poi 进行查询。函数
找到想要依赖的版本字体
点击进入后,能够直接复制里面的依赖spa
import org.apache.poi.sl.usermodel.SlideShow; import org.apache.poi.sl.usermodel.SlideShowFactory; SlideShow slideShow = SlideShowFactory.create(new File("./res/1.pptx"));
XSLFSlide未提供public的构造函数,获取只能经过SlideShow中提供的createSlide, getSlides方法获取。code
如图:XSLFSlide的构造函数,都是私有的xml
如图:SlideShow中提供的Slide获取方法, 因为 Slide 接口只有一个实现(XLSFSlide),因此能够直接转换成实现类(XLSFSlide)操做blog
获取全部的备注接口
XSLFNotes notes = slider.getNotes();
获取全部的批注图片
List<XSLFComment> comments = slider.getComments();
List<POIXMLDocumentPart.RelationPart> relationParts = slider.getRelationParts();
List<XSLFShape> shapes = slider.getShapes();
POIXMLDocmentPart是一个ppt关联的部分的具体内容,包括:备注、批注、图片、图表、母版等。
经过 POIXMLDocumentPart documentPart = POIXMLDocumentPart.RelationPart.getDocumentPart() 获取。
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; }
获取 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(); } }