需要一个关于如何获取DOC和DOCX文件的字数的明确示例

时间:2014-05-05 18:38:06

标签: java apache apache-poi docx doc

我能够读取DOC文件并获取其字数,但这是错误的。

我的代码:

 public class WordCounter {
    public static void main(String[] args) throws Throwable {
        processDOC();
    }

    private static void processDOC() throws Throwable {
        File file = new File("/Users/yjiang/Desktop/whatever.doc");
        File file2 = new File("/Users/yjiang/Desktop/Test.docx");
        File file3 = new File("/Users/yjiang/Desktop/QB Tests 4-14-2014.xls");
        File file4 = new File("/Users/yjiang/Desktop/QB Tests 4-14-2014.xlsx");

        try {
            FileInputStream fs = new FileInputStream(file);
            POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fs);
            DirectoryEntry directoryEntry = poifsFileSystem.getRoot();
            DocumentEntry documentEntry = (DocumentEntry) directoryEntry.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            DocumentInputStream dis = new DocumentInputStream(documentEntry);
            PropertySet ps = new PropertySet(dis);
            SummaryInformation si = new SummaryInformation(ps);

            System.out.println(si.getWordCount());
        } catch (Exception e) {
            e.printStackTrace();
        }


        try {
            HWPFDocument hwpfDocument = new HWPFDocument(new FileInputStream(file));
            System.out.println(hwpfDocument.getDocProperties().getCWords()); // actually 71 words using word count in MSWord, returned 57.
            System.out.println(hwpfDocument.getDocProperties().getCWordsFtnEnd());
            XWPFDocument xwpfDocument = new XWPFDocument(new FileInputStream(file2)); // actually 71 words using word count in MSWord, returned 57.
            System.out.println(xwpfDocument.getProperties().getExtendedProperties().getUnderlyingProperties().getWords());



            System.out.println();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

" whatever.doc"有71个单词,当我运行它时,它只返回57。

enter image description here

似乎我无法使用相同的方法来读取DOCX文件,当我运行它时,我得到以下内容:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)

可以提供一个例子吗?

1 个答案:

答案 0 :(得分:0)

我还发现内置的单词计数器给出了奇怪的计数,但文本提取似乎更可靠,所以我使用这个解决方案:

public long getWordCount(File file) throws IOException {
    POITextExtractor textExtractor;
    if (file.getName().endsWith(".docx")) {
        XWPFDocument doc = new XWPFDocument(new FileInputStream(file));
        textExtractor = new XWPFWordExtractor(doc);
    }
    else if (file.getName().endsWith(".doc")) {
        textExtractor = new WordExtractor(new FileInputStream(file));
    }
    else {
        throw new IllegalArgumentException("Not a MS Word file.");
    }

    return Arrays.stream(textExtractor.getText().split("\\s+"))
     .filter(s -> s.matches("^.*[\\p{L}\\p{N}].*$"))
     .count();
}

如果需要,底部的正则表达式可以进行调整,但总的来说,这一点已经证明具有相当的弹性。