开发手册 欢迎您!
软件开发者资料库

Apache POI Word - 段落

Apache POI Word段落 - 从简单和简单的步骤学习Apache POI Word,从基本到高级概念,包括概述,Apache POI安装,核心类,文档,段落,边框,表格,字体样式和对齐,文本提取。

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

完成本章后,您将能够创建段落并对其执行读取操作.

创建段落

首先,让我们使用前面章节中讨论的引用类创建段落.按照上一章的说法,先创建一个Document,然后我们就可以创建一个段落.

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

 //创建空白文档 XWPFDocument document = new XWPFDocument(); //创建一个空白电子表格 XWPFParagraph paragraph = document.createParagraph();

在段落上运行

您可以使用运行.使用段落实例,您可以创建运行.

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

XWPFRun run = paragraph.createRun();

写入段落

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

Atit1352.com, we strive hard to provide quality tutorials for self-learningpurpose in the domains of Academics, Information Technology, Management and ComputerProgramming Languages.

以下代码用于将上述数据写入段落.

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 IT屋.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,然后编译并运行它来自命令提示符,如下所示 :

$javac CreateParagraph.java$java CreateParagraph

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

createparagraph.docx written successfully

createparagraph.docx 文件如下所示.

创建段落