如何使用 Apache POI 3.5 中新的 OOXML 支持从 DOCX 文件中提取纯文本? [英] How to extract plain text from a DOCX file using the new OOXML support in Apache POI 3.5?
问题描述
2009 年 9 月 28 日,Apache POI 项目发布了 3.5 版,正式支持 Office 中引入的 OOXML 格式2007 年,如 DOCX 和 XLSX.
On September 28, 2009 the Apache POI project released version 3.5 which officially supports the OOXML formats introduced in Office 2007, like DOCX and XLSX.
请提供以纯文本形式提取 DOCX 文件内容的代码示例,忽略任何样式或格式.
Please provide a code sample for extracting a DOCX file's content in plain text, ignoring any styles or formatting.
我之所以这么问是因为我找不到任何涵盖新 OOXML 支持的 Apache POI 示例.
I am asking this because I have been unable to find any Apache POI examples covering the new OOXML support.
推荐答案
这对我有用.确保添加了所需的 jars(升级 xmlbeans 等)
This worked for me. Make sure you add the required jars (upgrade xmlbeans, etc.)
public String extractText(InputStream in) throws Exception {
XWPFDocument doc = new XWPFDocument(in);
XWPFWordExtractor ex = new XWPFWordExtractor(doc);
String text = ex.getText();
return text;
}
这篇关于如何使用 Apache POI 3.5 中新的 OOXML 支持从 DOCX 文件中提取纯文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!