- Apache POI PPT Tutorial
- Apache POI PPT - Home
- Apache POI PPT - Overview
- Apache POI PPT - Java API Flavors
- Apache POI PPT - Installation
- Apache POI PPT - Classes & Methods
- Apache POI PPT - Presentation
- Apache POI PPT - Slide Layouts
- Apache POI PPT - Slide Management
- Apache POI PPT - Images
- Apache POI PPT - Creating Hyperlinks
- Apache POI PPT - Reading Shapes
- Apache POI PPT - Formatting Text
- Apache POI PPT - Merging
- Apache POI PPT - PPT to Image
- Apache POI PPT Resources
- Apache POI PPT - Quick Guide
- Apache POI PPT - Useful Resources
- Apache POI PPT - Discussion
Apache POI PPT - 演示
一般来说,我们使用MS-PowerPoint来制作演示文稿。现在让我们看看如何使用 Java 创建演示文稿。完成本章后,您将能够创建新的 MS-PowerPoint 演示文稿并使用 Java 程序打开现有的 PPT。
创建空演示文稿
要创建空演示文稿,您必须实例化org.poi.xslf.usermodel包的XMLSlideShow类-
XMLSlideShow ppt = new XMLSlideShow();
使用FileOutputStream类保存对 PPT 文档的更改-
File file = new File("C://POIPPT//Examples//example1.pptx"); FileOutputStream out = new FileOutputStream(file); ppt.write(out);
下面给出的是创建空白 MS-PowerPoint 演示文稿的完整程序。
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; public class CreatePresentation { public static void main(String args[]) throws IOException { //creating a new empty slide show XMLSlideShow ppt = new XMLSlideShow(); //creating an FileOutputStream object File file = new File("example1.pptx"); FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); System.out.println("Presentation created successfully"); out.close(); } }
将上述 Java 代码保存为CreatePresentation.java,然后从命令提示符编译并执行它,如下所示 -
$javac CreatePresentation.java $java CreatePresentation
如果您的系统环境配置了 POI 库,它将编译并执行以在当前目录中生成名为example1.pptx的空白 PPT 文件,并在命令提示符下显示以下输出 -
Presentation created successfully
空白 PowerPoint 文档如下所示 -
编辑现有演示文稿
要打开现有演示文稿,请实例化XMLSlideShow类并将要编辑的文件的FileInputStream对象作为参数传递给XMLSlideShow构造函数。
File file = new File("C://POIPPT//Examples//example1.pptx"); FileInputstream inputstream = new FileInputStream(file); XMLSlideShow ppt = new XMLSlideShow(inputstream);
您可以使用org.poi.xslf.usermodel包中 XMLSlideShow 类的createSlide()方法将幻灯片添加到演示文稿中。
XSLFSlide slide1 = ppt.createSlide();
下面给出的是打开现有 PPT 并将幻灯片添加到现有 PPT 的完整程序 -
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; public class EditPresentation { public static void main(String ar[]) throws IOException { //opening an existing slide show File file = new File("example1.pptx"); FileInputStream inputstream = new FileInputStream(file); XMLSlideShow ppt = new XMLSlideShow(inputstream); //adding slides to the slideshow XSLFSlide slide1 = ppt.createSlide(); XSLFSlide slide2 = ppt.createSlide(); //saving the changes FileOutputStream out = new FileOutputStream(file); ppt.write(out); System.out.println("Presentation edited successfully"); out.close(); } }
将上述 Java 代码保存为EditPresentation.java,然后从命令提示符编译并执行它,如下所示 -
$javac EditPresentation.java $java EditPresentation
它将编译并执行以生成以下输出 -
slides successfully added
带有新添加幻灯片的输出 PPT 文档如下所示 -
添加幻灯片到PPT后,您可以对幻灯片进行添加、执行、读取和写入操作。