Apache Poi Ppt 简明教程

Apache POI PPT - Presentation

通常,我们使用 MS-PowerPoint 创建演示文稿。现在让我们看看如何使用 Java 创建演示文稿。完成本章后,你将能够创建新的 MS-PowerPoint 演示文稿,并使用你的 Java 程序打开现有的 PPT。

Generally, we use MS-PowerPoint to create presentations. Now let us see how to create presentations using Java. After completion of this chapter, you will be able to create new MS-PowerPoint presentations and open existing PPTs with your Java program.

Creating Empty Presentation

要创建一个空演示文稿,你必须实例化 org.poi.xslf.usermodel 包的 XMLSlideShow 类 −

To create an empty presentation, you have to instantiate the XMLSlideShow class of the org.poi.xslf.usermodel package −

XMLSlideShow ppt = new XMLSlideShow();

使用 FileOutputStream 类将更改保存到 PPT 文档 −

Save the changes to a PPT document using the FileOutputStream class −

File file = new File("C://POIPPT//Examples//example1.pptx");
FileOutputStream out = new FileOutputStream(file);
ppt.write(out);

下面给出了创建空白 MS-PowerPoint 演示文稿的完整程序。

Given below is the complete program to create a blank MS-PowerPoint presentation.

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 ,然后从命令提示符编译并执行它,如下所示 −

Save the above Java code as CreatePresentation.java, and then compile and execute it from the command prompt as follows −

$javac CreatePresentation.java
$java CreatePresentation

如果你的系统环境配置了 POI 库,它将编译并执行以在你的当前目录中生成一个名为 example1.pptx 的空白 PPT 文件,并在命令提示符上显示以下输出 −

If your system environment is configured with the POI library, it will compile and execute to generate a blank PPT file named example1.pptx in your current directory and display the following output on the command prompt −

Presentation created successfully

空白的 PowerPoint 文档显示如下 −

The blank PowerPoint document appears as follows −

example1

Editing an Existing Presentation

要打开现有的演示文稿,请实例化 XMLSlideShow 类,并将要编辑的文件的 FileInputStream 对象作为 XMLSlideShow 构造函数的参数传递。

To open an existing presentation, instantiate the XMLSlideShow class and pass the FileInputStream object of the file to be edited, as an argument to the XMLSlideShow constructor.

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() 方法向演示文稿中添加幻灯片。

You can add slides to a presentation using the createSlide() method of the XMLSlideShow class which is in the org.poi.xslf.usermodel package.

XSLFSlide slide1 = ppt.createSlide();

下面给出了打开现有的 PPT 并向其中添加幻灯片的完整程序 −

Given below is the complete program to open and add slides to an existing 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 ,然后从命令提示符编译并执行它,如下所示 −

Save the above Java code as EditPresentation.java, and then compile and execute it from the command prompt as follows −

$javac EditPresentation.java
$java EditPresentation

它将编译和执行以生成以下输出 −

It will compile and execute to generate the following output −

slides successfully added

带有新添加幻灯片输出的 PPT 文档如下所示 −

The output PPT document with newly added slides looks as follows −

editexample1

将幻灯片添加到 PPT 后,你可以在幻灯片上添加、执行、读取和写入操作。

After adding slides to a PPT, you can add, perform, read, and write operations on the slides.