- Apache POI Word Tutorial
- Apache POI Word - Home
- Apache POI Word - Overview
- Apache POI Word - Installation
- Apache POI Word - Core Classes
- Apache POI Word - Document
- Apache POI Word - Paragraph
- Apache POI Word - Borders
- Apache POI Word - Tables
- Apache POI Word - Font & Alignment
- Apache POI Word - Text Extraction
- Apache POI Word Resources
- Apache POI Word - Quick Guide
- Apache POI Word - Useful Resources
- Apache POI Word - Discussion
Apache POI Word - 文本提取
本章介绍如何使用 Java 从 Word 文档中提取简单的文本数据。如果您想从 Word 文档中提取元数据,请使用 Apache Tika。
对于 .docx 文件,我们使用 org.apache.poi.xwpf.extractor.XPFFWordExtractor 类从 Word 文件中提取并返回简单数据。同样,我们有不同的方法从 Word 文件中提取标题、脚注、表格数据等。
以下代码显示了如何从 Word 文件中提取简单文本 -
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。从命令提示符编译并执行它,如下所示 -
$javac WordExtractor.java $java WordExtractor
它将生成以下输出 -
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.