如何将docx文件中的所有单词都放到dictonary中

时间:2017-06-21 08:57:56

标签: c# dictionary ms-word docx

我想从docx(WORD)文件中提取所有单词并将它们放在字典中,因为我试图将它们作为键,但是有时候在docx文件中有相同的单词,因此字典的所有值都有。

 ` Dictionary <int, string> motRap = new Dictionary<int, string>();
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
            Document document = application.Documents.Open("test.docx");

        // Loop through all words in the document.
        int count = document.Words.Count;
        for (int i = 1; i <= count; i++)
        {
            string text = document.Words[i].Text;
            motRap.Add(i, text);

        }
        // Close word.
        application.Quit();
        foreach(var o in motRap)
        {
            Console.WriteLine(o.Value);
        }`

当我尝试使用一个小的docx文件时,它可以工作,但是现在当我尝试使用更大的docx文件时,它会说有一个未处理的异常。有没有更好的方法从docx文件中提取单词?

0 个答案:

没有答案