Apache Poi Word 简明教程
Apache POI Word - Overview
很多时候,软件应用程序需要生成 Microsoft Word 文件格式的参考文档。有时,甚至预期一个应用程序接收 Word 文件作为输入数据。
Many a time, a software application is required to generate reference documents in Microsoft Word file format. Sometimes, an application is even expected to receive Word files as input data.
任何想要生成 MS-Office 文件作为输出的 Java 编程人员都必须使用一个预定义且只读的 API 来执行此操作。
Any Java programmer who wants to produce MS-Office files as output must use a predefined and read-only API to do so.
What is Apache POI?
Apache POI 是一个流行的 API,它允许编程人员使用 Java 程序创建、修改和显示 MS-Office 文件。它是一个由 Apache 软件基金会开发和分发的开源库,用于使用 Java 程序设计或修改 MS-Office 文件。它包含将用户输入数据或文件解码为 MS-Office 文档的类和方法。
Apache POI is a popular API that allows programmers to create, modify, and display MS-Office files using Java programs. It is an open source library developed and distributed by Apache Software Foundation to design or modify MS-Office files using Java program. It contains classes and methods to decode the user input data or a file into MS-Office documents.
Components of Apache POI
Apache POI 包含用于操作 MS-Office 所有 OLE2 复合文档的类和方法。此 API 的组件列表如下 -
Apache POI contains classes and methods to work on all OLE2 Compound documents of MS-Office. The list of components of this API is given below −
-
POIFS (Poor Obfuscation Implementation File System) − This component is the basic factor of all other POI elements. It is used to read different files explicitly.
-
HSSF (Horrible SpreadSheet Format) − It is used to read and write .xls format of MS-Excel files.
-
XSSF (XML SpreadSheet Format) − It is used for .xlsx file format of MS-Excel.
-
HPSF (Horrible Property Set Format) − It is used to extract property sets of the MS-Office files.
-
HWPF (Horrible Word Processor Format) − It is used to read and write .doc extension files of MS-Word.
-
XWPF (XML Word Processor Format) − It is used to read and write .docx extension files of MS-Word.
-
HSLF (Horrible Slide Layout Format) − It is used to read, create, and edit PowerPoint presentations.
-
HDGF (Horrible DiaGram Format) − It contains classes and methods for MS-Visio binary files.
-
HPBF (Horrible PuBlisher Format) − It is used to read and write MS-Publisher files.
本教程将指导您完成使用 Java 处理 MS-Word 文件的过程。因此,讨论仅限于 HWPF 和 XWPF 组件。
This tutorial guides you through the process of working on MS-Word files using Java. Therefore the discussion is confined to HWPF and XWPF components.
Note − POI 的较旧版本支持 DOC、XLS、PPT 等二进制文件格式。从版本 3.5 开始,POI 支持 DOCX、XLS、PPTX 等 MS-Office 的 OOXML 文件格式。
Note − OLDER VERSIONS OF POI SUPPORT BINARY FILE FORMATS SUCH AS DOC, XLS, PPT, ETC. VERSION 3.5 ONWARDS, POI SUPPORTS OOXML FILE FORMATS OF MS-OFFICE SUCH AS DOCX, XLSX, PPTX, ETC.
Apache POI Word - Installation
本教程将指导你完成在 Windows 和 Linux 系统上设置 Apache POI 的流程。Apache POI 可以在不执行任何复杂设置程序的情况下,使用几个简单的步骤轻松安装并集成到当前的 Java 环境中。在安装时需要有用户管理权限。
This chapter takes you through the process of setting up Apache POI on Windows and Linux based systems. Apache POI can be easily installed and integrated with your current Java environment following a few simple steps without any complex setup procedures. User administration is required while installation.
System Requirements
JDK |
Java SE 2 JDK 1.5 or above |
Memory |
1 GB RAM (recommended) |
Disk Space |
No minimum requirement |
Operating System Version |
Windows XP or above, Linux |
现在让我们继续进行安装 Apache POI 的步骤。
Let us now proceed with the steps to install Apache POI.
Step 1: Verify your Java Installation
Step 1: Verify your Java Installation
首先,你的系统中需要安装 Java 软件开发工具包 (SDK)。要验证这一点,请根据所使用的平台执行以下两个命令。
First of all, you need to have Java Software Development Kit (SDK) installed on your system. To verify this, execute any of the two commands depending on the platform you are working on.
如果 Java 安装已正确完成,则它将显示 Java 安装的当前版本和规范。以下表中给出了一个示例输出。
If the Java installation has been done properly, then it will display the current version and specification of your Java installation. A sample output is given in the following table.
Platform |
Command |
Sample Output |
Windows |
Open command console and type − >java -version |
java version "11.0.11" 2021-04-20 LTS Java™ SE Runtime Environment 18.9 (build 11.0.11+9-LTS-194) Java HotSpot™ 64-Bit Server VM 18.9 (build 11.0.11+9-LTS-194, mixed mode) |
Linux |
Open command terminal and type − $java -version |
java version "11.0.11" 2021-04-20 LTS Open JDK Runtime Environment 18.9 (build 11.0.11+9-LTS-194) Open JDK 64-Bit Server VM (build 11.0.11+9-LTS-194, mixed mode) |
-
We assume the readers of this tutorial have Java SDK version 11.0.11 installed on their system.
-
In case you do not have Java SDK, download its current version from www.oracle.com/technetwork/java/javase/downloads/index.html and have it installed.
Step 2: Set your Java Environment
Step 2: Set your Java Environment
设置环境变量 JAVA_HOME 以指向计算机上安装 Java 的基本目录位置。例如,
Set the environment variable JAVA_HOME to point to the base directory location where Java is installed on your machine. For example,
Sr.No. |
Platform & Description |
1 |
Windows Set JAVA_HOME to C:\ProgramFiles\java\jdk11.0.11 |
2 |
Linux Export JAVA_HOME = /usr/local/java-current |
将 Java 编译器位置的完整路径附加到系统路径。
Append the full path of Java compiler location to the System Path.
Sr.No. |
Platform & Description |
1 |
Windows Append the String "C:\Program Files\Java\jdk11.0.11\bin" to the end of the system variable PATH. |
2 |
Linux Export PATH = $PATH:$JAVA_HOME/bin/ |
如上所述,从命令提示符执行命令 java -version 。
Execute the command java -version from the command prompt as explained above.
Step 3: Install Apache POI Library
Step 3: Install Apache POI Library
从 https://poi.apache.org/download.html 下载最新版本的 Apache POI,并将其内容解压缩到一个文件夹中,然后可以将该文件夹中的所需库连接到你的 Java 程序。我们假设这些文件保存在 C 驱动器的文件夹中。
Download the latest version of Apache POI from https://poi.apache.org/download.html and unzip its contents to a folder from where the required libraries can be linked to your Java program. Let us assume the files are collected in a folder on C drive.
将必需的 jars 的完整路径添加到 CLASSPATH,如下所示。
Add the complete path of the required jars as shown below to the CLASSPATH.
Sr.No. |
Platform & Description |
1 |
Windows Append the following strings to the end of the user variable CLASSPATH − C:\poi-bin-5.1.0\poi-5.1.0.jar; C:\poi-bin-5.1.0\poi-ooxml-5.1.0.jar; C:\poi-bin-5.1.0\poi-ooxml-full-5.1.0.jar; C:\poi-bin-5.1.0\lib\commons-codec-1.15.jar; C:\poi-bin-5.1.0\lib\commons-collections4-4.4.jar; C:\poi-bin-5.1.0\lib\commons-io-2.11.0.jar; C:\poi-bin-5.1.0\lib\commons-math3-3.6.1.jar; C:\poi-bin-5.1.0\lib\log4j-api-2.14.1.jar; C:\poi-bin-5.1.0\lib\SparseBitSet-1.2.jar; C\poi-bin-5.1.0\ooxml-lib\commons-compress-1.21.jar C\poi-bin-5.1.0\ooxml-lib\commons-logging-1.2.jar C\poi-bin-5.1.0\ooxml-lib\curvesapi-1.06.jar C\poi-bin-5.1.0\ooxml-lib\slf4j-api-1.7.32.jar C\poi-bin-5.1.0\ooxml-lib\xmlbeans-5.0.2.jar |
2 |
Linux Export CLASSPATH = $CLASSPATH: /usr/share/poi-bin-5.1.0/poi-5.1.0.jar.tar: /usr/share/poi-bin-5.1.0/poi-ooxml-5.1.0.tar: /usr/share/poi-bin-5.1.0/poi-ooxml-full-5.1.0.tar: /usr/share/poi-bin-5.1.0/lib/commons-codec-1.15.jar.tar: /usr/share/poi-bin-5.1.0/lib/commons-collections4-4.4.tar: /usr/share/poi-bin-5.1.0/lib/commons-io-2.11.0.tar: /usr/share/poi-bin-5.1.0/lib/commons-math3-3.6.1.tar: /usr/share/poi-bin-5.1.0/lib/log4j-api-2.14.1.tar: /usr/share/poi-bin-5.1.0/lib/SparseBitSet-1.2.tar: /usr/share/poi-bin-5.1.0/ooxml-lib/commons-compress-1.21.tar: /usr/share/poi-bin-5.1.0/ooxml-lib/commons-logging-1.2.tar: /usr/share/poi-bin-5.1.0/ooxml-lib/curvesapi-1.06.tar: /usr/share/poi-bin-5.1.0/ooxml-lib/slf4j-api-1.7.32.tar: /usr/share/poi-bin-5.1.0/ooxml-lib/xmlbeans-5.0.2.tar: |
Apache POI Word - Core Classes
本章带您了解 Apache POI 的类和方法,用于管理 Word 文档。
This chapter takes you through the classes and methods of Apache POI for managing a Word document.
Document
这是一个标记接口(接口不包含任何方法),它通知已实现的类可以创建 Word 文档。
This is a marker interface (interface do not contain any methods), that notifies that the implemented class can be able to create a word document.
XWPFDocument
这是一个在 org.apache.poi.xwpf.usermodel 包下的类。它用于创建带 .docx 文件格式的 MS-Word 文档。
This is a class under org.apache.poi.xwpf.usermodel package. It is used to create MS-Word Document with .docx file format.
Class Methods
Sr.No. |
Method & Description |
1 |
commit() Commits and saves the document. |
2 |
createParagraph() Appends a new paragraph to this document. |
3 |
createTable() Creates an empty table with one row and one column as default. |
4 |
createTOC() Creates a table of content for Word document. |
5 |
getParagraphs() Returns the paragraph(s) that holds the text of the header or footer. |
6 |
getStyle() Returns the styles object used. |
有关此类的其余方法,请参阅完整 API 文档 −
For the remaining methods of this class, refer the complete API document at −
XWPFParagraph
这是一个在 org.apache.poi.xwpf.usermodel 包下的类,用于在 Word 文档中创建段落。此实例还用于向 Word 文档中添加所有类型的元素。
This is a class under org.apache.poi.xwpf.usermodel package and is used to create paragraph in a word document. This instance is also used to add all types of elements into word document.
Class Methods
Sr.No. |
Method & Description |
1 |
createRun() Appends a new run to this paragraph. |
2 |
getAlignment() Returns the paragraph alignment which shall be applied to the text in this paragraph. |
3 |
setAlignment(ParagraphAlignment align) Specifies the paragraph alignment which shall be applied to the text in this paragraph. |
4 |
setBorderBottom(Borders border) Specifies the border which shall be displayed below a set of paragraphs, which have the same set of paragraph border settings. |
5 |
setBorderLeft(Borders border) Specifies the border which shall be displayed on the left side of the page around the specified paragraph. |
6 |
setBorderRight(Borders border) Specifies the border which shall be displayed on the right side of the page around the specified paragraph. |
7 |
setBorderTop(Borders border) Specifies the border which shall be displayed above a set of paragraphs which have the same set of paragraph border settings. |
有关此类的其余方法,请参阅完整 API 文档 −
For the remaining methods of this class, refer the complete API document at −
XWPFRun
这是 org.apache.poi.xwpf.usermodel 包下的一个类,用于向段落中添加一个文本区域。
This is a class under org.apache.poi.xwpf.usermodel package and is used to add a region of text to the paragraph.
Class Methods
Sr.No. |
Method & Description |
1 |
addBreak() Specifies that a break shall be placed at the current location in the run content. |
2 |
addTab() Specifies that a tab shall be placed at the current location in the run content. |
3 |
setColor(java.lang.String rgbStr) Sets text color. |
4 |
setFontSize(int size) Specifies the font size which shall be applied to all noncomplex script characters in the content of this run when displayed. |
5 |
setText(java.lang.String value) Sets the text of this text run. |
6 |
setBold(boolean value) Specifies whether the bold property shall be applied to all non-complex script characters in the content of this run when displayed in a document. |
有关此类的其余方法,请参阅完整 API 文档 −
For the remaining methods of this class, refer the complete API document at −
XWPFStyle
这是 org.apache.poi.xwpf.usermodel 包下的一个类,用于向 Word 文档中的对象元素添加不同的样式。
This is a class under org.apache.poi.xwpf.usermodel package and is used to add different styles to the object elements in a word document.
Class Methods
Sr.No. |
Method & Description |
1 |
getNextStyleID() It is used to get StyleID of the next style. |
2 |
getStyleId() It is used to get StyleID of the style. |
3 |
getStyles() It is used to get styles. |
4 |
setStyleId(java.lang.String styleId) It is used to set styleID. |
有关此类的其余方法,请参阅完整 API 文档 −
For the remaining methods of this class, refer the complete API document at −
XWPFTable
这是 org.apache.poi.xwpf.usermodel 包下的一个类,用于将表格数据添加到 Word 文档中。
This is a class under org.apache.poi.xwpf.usermodel package and is used to add table data into a word document.
Class Methods
Sr.No. |
Method & Description |
1 |
addNewCol() Adds a new column for each row in this table. |
2 |
addRow(XWPFTableRow row, int pos) Adds a new Row to the table at position pos. |
3 |
createRow() Creates a new XWPFTableRow object with as many cells as the number of columns defined in that moment. |
4 |
setWidth(int width) Sets the width of the column. |
有关此类的其余方法,请参阅以下完整 API 文档 − POI API Documentation
For the remaining methods of this class, refer the complete API document at − POI API Documentation
XWPFWordExtractor
这是 org.apache.poi.xwpf.extractor 包下的一个类。它是一个基本解析类,用于从 Word 文档中提取简单文本。
This is a class under org.apache.poi.xwpf.extractor package. It is a basic parser class used to extract the simple text from a Word document.
Class Methods
Sr.No. |
Method & Description |
1 |
getText() Retrieves all the text from the document. |
有关此类的其余方法,请参阅以下完整 API 文档 − POI API Documentation
For the remaining methods of this class, refer the complete API document at − POI API Documentation
Apache POI Word - Document
此处,术语“文档”指的是 MS-Word 文件。完成本章后,你将能够创建新文档并使用 Java 程序打开现有文档。
Here the term 'document' refers to a MS-Word file. After completion of this chapter, you will be able to create new documents and open existing documents using your Java program.
Create Blank Document
以下简单程序用于创建一个空白 MS-Word 文档 −
The following simple program is used to create a blank MS-Word document −
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class CreateDocument {
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("createdocument.docx"));
document.write(out);
out.close();
System.out.println("createdocument.docx written successully");
}
}
将以上 Java 代码保存为 CreateDocument.java, ,然后从命令提示符编译并执行,如下所示 −
Save the above Java code as CreateDocument.java, and then compile and execute it from the command prompt as follows −
$javac CreateDocument.java
$java CreateDocument
如果您的系统环境已配置 POI 库,它将编译并执行,在当前目录中生成一个名为 createdocument.docx 的空白 Word 文档文件,并在命令提示符中显示以下输出 −
If your system environment is configured with the POI library, it will compile and execute to generate a blank Word document file named createdocument.docx in your current directory and display the following output in the command prompt −
createdocument.docx written successfully
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.
Apache POI Word - Borders
在本章中,您将学习如何使用 Java 编程为段落应用边框。
In this chapter, you will learn how to apply border to a paragraph using Java programming.
Applying Border
以下代码用于在文档中应用边框 −
The following code is used to apply Borders in a Document −
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.Borders;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class ApplyingBorder {
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("applyingborder.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set bottom border to paragraph
paragraph.setBorderBottom(Borders.BASIC_BLACK_DASHES);
//Set left border to paragraph
paragraph.setBorderLeft(Borders.BASIC_BLACK_DASHES);
//Set right border to paragraph
paragraph.setBorderRight(Borders.BASIC_BLACK_DASHES);
//Set top border to paragraph
paragraph.setBorderTop(Borders.BASIC_BLACK_DASHES);
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("applyingborder.docx written successully");
}
}
将上述代码保存在名为 ApplyingBorder.java, 的文件中,并从命令提示符按以下方式编译并执行它 −
Save the above code in a file named ApplyingBorder.java, compile and execute it from the command prompt as follows −
$javac ApplyingBorder.java
$java ApplyingBorder
如果您的系统已配置 POI 库,那么它将编译并执行,在当前目录中生成一个名为 applyingborder.docx 的 Word 文档,并显示以下输出 −
If your system is configured with the POI library, then it will compile and execute to generate a Word document named applyingborder.docx in your current directory and display the following output −
applyingborder.docx written successfully
applyingborder.docx 文件看起来如下 −
The applyingborder.docx file looks as follows −
Apache POI Word - Tables
在本章中,您将学习如何在文档中创建数据表。您可以使用 XWPFTable 类创建表数据。通过将每个 Row 添加到表中并将每个 cell 添加到 Row, 中,您将获得表数据。
In this chapter, you will learn how to create a table of data in a document. You can create a table data by using XWPFTable class. By adding each Row to table and adding each cell to Row, you will get table data.
Create Table
以下代码用于在文档中创建表 −
The following code is used to creating table in a document −
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class CreateTable {
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("create_table.docx"));
//create table
XWPFTable table = document.createTable();
//create first row
XWPFTableRow tableRowOne = table.getRow(0);
tableRowOne.getCell(0).setText("col one, row one");
tableRowOne.addNewTableCell().setText("col two, row one");
tableRowOne.addNewTableCell().setText("col three, row one");
//create second row
XWPFTableRow tableRowTwo = table.createRow();
tableRowTwo.getCell(0).setText("col one, row two");
tableRowTwo.getCell(1).setText("col two, row two");
tableRowTwo.getCell(2).setText("col three, row two");
//create third row
XWPFTableRow tableRowThree = table.createRow();
tableRowThree.getCell(0).setText("col one, row three");
tableRowThree.getCell(1).setText("col two, row three");
tableRowThree.getCell(2).setText("col three, row three");
document.write(out);
out.close();
System.out.println("create_table.docx written successully");
}
}
将以上代码保存到名为 CreateTable.java. 的文件中,从命令提示符下编译并执行,如下所示 −
Save the above code in a file named CreateTable.java. Compile and execute it from the command prompt as follows −
$javac CreateTable.java
$java CreateTable
它在当前目录中生成一个名为 createtable.docx 的 Word 文件,并在命令提示符上显示以下输出 −
It generates a Word file named createtable.docx in your current directory and display the following output on the command prompt −
createtable.docx written successfully
createtable.docx 文件如下所示 −
The createtable.docx file looks as follows −
Apache POI Word - Font & Alignment
本章演示了如何使用 Java 在 Word 文档中应用不同的字体样式和对齐方式。通常,字体样式包含:字体大小、类型、粗体、斜体和下划线。对齐方式分为左、中、右和两端对齐。
This chapter shows how to apply different font styles and alignments in a Word document using Java. Generally, Font Style contains: Font size, Type, Bold, Italic, and Underline. And Alignment is categorized into left, center, right, and justify.
Font Style
以下代码用于设置不同的字体样式 −
The following code is used to set different styles of font −
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.VerticalAlign;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class FontStyle {
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("fontstyle.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set Bold an Italic
XWPFRun paragraphOneRunOne = paragraph.createRun();
paragraphOneRunOne.setBold(true);
paragraphOneRunOne.setItalic(true);
paragraphOneRunOne.setText("Font Style");
paragraphOneRunOne.addBreak();
//Set text Position
XWPFRun paragraphOneRunTwo = paragraph.createRun();
paragraphOneRunTwo.setText("Font Style two");
paragraphOneRunTwo.setTextPosition(100);
//Set Strike through and Font Size and Subscript
XWPFRun paragraphOneRunThree = paragraph.createRun();
paragraphOneRunThree.setStrike(true);
paragraphOneRunThree.setFontSize(20);
paragraphOneRunThree.setSubscript(VerticalAlign.SUBSCRIPT);
paragraphOneRunThree.setText(" Different Font Styles");
document.write(out);
out.close();
System.out.println("fontstyle.docx written successully");
}
}
将以上代码保存为 FontStyle.java ,然后从命令提示符下编译并执行,如下所示 −
Save the above code as FontStyle.java and then compile and execute it from the command prompt as follows −
$javac FontStyle.java
$java FontStyle
它将在当前目录生成一个名为 fontstyle.docx 的 Word 文件,并在命令提示符中显示以下输出 −
It will generate a Word file named fontstyle.docx in your current directory and display the following output on the command prompt −
fontstyle.docx written successfully
fontstyle.docx 文件如下所示。
The fontstyle.docx file looks as follows.
Alignment
使用以下代码将对齐方式设为段落文本 −
The following code is used to set alignment to the paragraph text −
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class AlignParagraph {
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("alignparagraph.docx"));
//create paragraph
XWPFParagraph paragraph = document.createParagraph();
//Set alignment paragraph to RIGHT
paragraph.setAlignment(ParagraphAlignment.RIGHT);
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.");
//Create Another paragraph
paragraph = document.createParagraph();
//Set alignment paragraph to CENTER
paragraph.setAlignment(ParagraphAlignment.CENTER);
run = paragraph.createRun();
run.setText("The endeavour started by Mohtashim, an AMU " +
"alumni, who is the founder and the managing director " +
"of Tutorials Point (I) Pvt. Ltd. He came up with the " +
"website tutorialspoint.com in year 2006 with the help" +
"of handpicked freelancers, with an array of tutorials" +
" for computer programming languages. ");
document.write(out);
out.close();
System.out.println("alignparagraph.docx written successfully");
}
}
将上述代码另存为 AlignParagraph.java ,然后从命令提示符编译并执行它,如下所示 −
Save the above code as AlignParagraph.java and then compile and execute it from the command prompt as follows −
$javac AlignParagraph.java
$java AlignParagraph
它将在当前目录生成一个名为 alignparagraph.docx 的 Word 文件,并在命令提示符中显示以下输出 −
It will generate a Word file named alignparagraph.docx in your current directory and display the following output in the command prompt −
alignparagraph.docx written successfully
alignparagraph.docx 文件如下所示 −
The alignparagraph.docx file looks as follows −
Apache POI Word - Text Extraction
本章说明如何使用 Java 从 Word 文档中提取简单文本数据。如果你想从 Word 文档中提取元数据,请使用 Apache Tika。
This chapter explains how to extract simple text data from a Word document using Java. In case you want to extract metadata from a Word document, make use of Apache Tika.
对于 .docx 文件,我们使用类 org.apache.poi.xwpf.extractor.XPFFWordExtractor ,它可以从 Word 文件中提取并返回简单数据。同样,我们有不同的方法来从 Word 文件中提取标题、脚注、表格数据等。
For .docx files, we use the class org.apache.poi.xwpf.extractor.XPFFWordExtractor that extracts and returns simple data from a Word file. In the same way, we have different methodologies to extract headings, footnotes, table data, etc. from a Word file.
以下代码演示如何从 Word 文件中提取简单文本 −
The following code shows how to extract simple text from a Word file −
import java.io.FileInputStream;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class WordExtractor {
public static void main(String[] args)throws Exception {
XWPFDocument docx = new XWPFDocument(new FileInputStream("createparagraph.docx"));
//using XWPFWordExtractor Class
XWPFWordExtractor we = new XWPFWordExtractor(docx);
System.out.println(we.getText());
}
}
将上述代码另存为 WordExtractor.java. ,然后从命令提示符编译并执行它,如下所示 −
Save the above code as WordExtractor.java. Compile and execute it from the command prompt as follows −
$javac WordExtractor.java
$java WordExtractor
它将生成以下输出 −
It will generate the following output −
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.