java - java中的getText().replaceAll()错误

标签 java apache-poi gettext illegalargumentexception replaceall

我正在从 WordExtractor 类 (apache POI) 中提取文本,但某些 .doc 文件出现错误。调试了一下,发现有问题的行是这里的最后一行:

HWPFDocument docx = new HWPFDocument(new FileInputStream(file));
WordExtractor we = new WordExtractor(docx);
String T = we.getText().replaceAll("\\n", " ").replaceAll("\\r", " ");

对于大多数 .docx.doc 文件,它工作正常。

错误信息是:

Exception in thread "main" java.lang.RuntimeException: 
java.lang.IllegalArgumentException: The end (4958) must not be before the start (4990)

如何修复它?

最佳答案

来自docs的XWPFWordExtractor :

Helper class to extract text from an OOXML Word file

所以这就是你的问题:)以及他们的解决方案 docs :

For .doc files from Word 97 - Word 2003, in scratchpad there is org.apache.poi.hwpf.extractor.WordExtractor, which will return text for your document.

Those using POI 3.7 can also extract simple textual content from older Word 6 and Word 95 files, using the scratchpad class org.apache.poi.hwpf.extractor.Word6Extractor.

For .docx files, the relevant class is org.apache.poi.xwpf.extractor.XPFFWordExtractor

关于java - java中的getText().replaceAll()错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41488362/

相关文章:

java - 有没有人有 Apache POI 将 PPTX 转换为 PNG 的示例

php - 如何在 JavaScript 中使用值 (PHP)

localization - 为什么 msgmerge 将我的一些翻译标记为模糊?

java - 安卓 Java : getText can't read superscript font

java - PJSUA2 Java - 如何获得正在进行的通话的音频媒体

java - 将逗号分隔的字符串传递给存储过程中的 IN 子句

java - 创建名称为 'entityManagerFactory' : [PersistenceUnit: default] Unable to build Hibernate SessionFactory 的 bean 时出错

java - HSSF 兴趣点 : How to know if data in cell is of Type Date?

java - Java 中将 HashMap 列表转换为带有换行符的 JSON 字符串

java - 使用 Apache POI 如何读取特定的 excel 列