Apache Poi Word 简明教程

Apache POI Word - Paragraph

本章您将学习如何创建一个段落以及如何使用 Java 将其添加到文档中。段落是 Word 文件中一页的一部分。

In this chapter you will learn how to create a Paragraph and how to add it to a document using Java. Paragraph is a part of a page in a Word file.

完成本课后,您将能够创建一个段落并在其上执行读取操作。

After completing this chapter, you will be able to create a Paragraph and perform read operations on it.

Create a Paragraph

首先,让我们使用前面章节中讨论的引用类创建一个段落。按照前面一章,首先创建一个文档,然后我们就可以创建一个段落。

First of all, let us create a Paragraph using the referenced classes discussed in the earlier chapters. By following the previous chapter, create a Document first, and then we can create a Paragraph.

以下代码片段用于创建电子表格 −

The following code snippet is used to create a spreadsheet −

//Create Blank document
XWPFDocument document = new XWPFDocument();

//Create a blank spreadsheet
XWPFParagraph paragraph = document.createParagraph();

Run on Paragraph

您可以使用 Run 输入文本或任何对象元素。使用段落实例,您可以创建 run

You can enter the text or any object element, using Run. Using Paragraph instance you can create run.

以下代码片段用于创建运行。

The following code snippet is used to create a Run.

XWPFRun run = paragraph.createRun();

Write into a Paragraph

让我们尝试在文档中输入一些文本。请考虑以下文本数据 −

Let us try entering some text into a document. Consider the below text data −

At tutorialspoint.com, we strive hard to provide quality tutorials for self-learning purpose
in the domains of Academics, Information Technology, Management and Computer Programming Languages.

以下代码用于将以上数据写入一个段落。

The following code is used to write the above data into a paragraph.

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;

public class CreateParagraph {
   public static void main(String[] args)throws Exception {
      //Blank Document
      XWPFDocument document = new XWPFDocument();

      //Write the Document in file system
      FileOutputStream out = new FileOutputStream(new File("createparagraph.docx"));

      //create Paragraph
      XWPFParagraph paragraph = document.createParagraph();
      XWPFRun run = paragraph.createRun();
      run.setText("At tutorialspoint.com, we strive hard to " +
         "provide quality tutorials for self-learning " +
         "purpose in the domains of Academics, Information " +
         "Technology, Management and Computer Programming Languages.");

      document.write(out);
      out.close();
      System.out.println("createparagraph.docx written successfully");
   }
}

将以上 Java 代码保存为 CreateParagraph.java, ,然后从命令提示符下编译并运行,如下所示 −

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

$javac CreateParagraph.java
$java CreateParagraph

它将编译并执行,在当前目录中生成名为 createparagraph.docx 的 Word 文件,您将在命令提示符中获得以下输出 −

It will compile and execute to generate a Word file named createparagraph.docx in your current directory and you will get the following output in the command prompt −

createparagraph.docx written successfully

createparagraph.docx 文件如下所示。

The createparagraph.docx file looks as follows.

createparagraph