0

我正在尝试获取Stringword 文档中包含的文本。我尝试使用Apache POIapi 的代码是:

FileInputStream fis = new FileInputStream(file.getAbsolutePath());
        HWPFDocument document = new HWPFDocument(fis);
        WordExtractor extractor = new WordExtractor(document);
        String fileData = extractor.getText();

fileData应该包含来自word文件的数据。

但是我得到了一些我想消除的无效字符。例如,word 中的以下文本:

Project Name    Customer 360

Position        Software Engineer

在 java 控制台中打印时出现:

Project Name [?]Customer 360[?][?]Position \t [?]Software Engineer

[?]一个小盒子里的问号符号在哪里。当我把它贴在这里时,它没有出现,所以我只是用来[?]表示它。

我希望输出是这样的:

Project Name \t Customer 360 \n Position \t Software Engineer

这为我提供了有关处理此文本真正需要的选项卡和新行的信息。

我知道tab并且newline信息在我得到的时候就在那里\t\n在某些地方,但在某些地方却不见了。

4

1 回答 1

1

看起来您有一些特殊字段应用于该文本。很可能它具有适用于它的链接、特殊规则、表单字段等

如果您不想要所有这些,您需要使用WordExtractor 上的 stripFields(java.lang.String) 方法来删​​除它们,只留下显示的文本。

该方法的JavaDoc

从字符串中删除任何字段(例如宏、页标记等)。

您的代码将是:

    FileInputStream fis = new FileInputStream(file.getAbsolutePath());
    HWPFDocument document = new HWPFDocument(fis);
    WordExtractor extractor = new WordExtractor(document);
    String rawText = extractor.getText();
    String displayText = extractor.stripFields(rawText);
于 2013-10-16T11:04:58.667 回答