【问题标题】:How to extract text data from MS-Word doc file如何从 MS-Word doc 文件中提取文本数据
【发布时间】:2013-02-13 10:15:27
【问题描述】:

我正在开发一个简历存档,人们可以在其中上传他们的简历,并且该简历将保存在特定位置。最重要的是人们可以使用任何版本的 MS-word 来准备他们的简历,并且简历文件的扩展名可以是 doc 或 docx。所以我只想知道是否有任何可用的免费库可用于从 doc 或 docx 文件中提取文本数据,该库在所有 ms-word 版本的情况下都可以使用,如果 ms-word 未安装在 pc 中也可以使用。我搜索谷歌,发现一些文章从 doc 文件中提取文本数据,但我不确定它们是否适用于所有 ms-word 版本。所以请指导我应该使用哪个库从 ms-word 中提取数据的信息,无论 ms-word 版本如何,也给我一些关于这个问题的好文章链接。

还指导我是否有任何可用的查看器可用于显示我的 c# 应用程序中的 doc 文件内容,而与 ms-word 版本无关。 谢谢

我得到了答案

**Need to add this reference Microsoft.Office.Interop.Word**

using System.Runtime.InteropServices.ComTypes;
using System.IO;

       public static string GetText(string strfilename)
    {
        string strRetval = "";
        System.Text.StringBuilder strBuilder = new System.Text.StringBuilder();
        if (File.Exists(strfilename))
        {
            try
            {
                using (StreamReader sr = File.OpenText(strfilename))
                {
                    string s = "";
                    while ((s = sr.ReadLine()) != null)
                    {
                        strBuilder.AppendLine(s);
                    }
                }
            }
            catch (Exception ex)
            {
                SendErrorMail(ex);
            }
            finally
            {
                if (System.IO.File.Exists(strfilename))
                    System.IO.File.Delete(strfilename);
            }
        }

        if (strBuilder.ToString().Trim() != "")
            strRetval = strBuilder.ToString();
        else
            strRetval = "";

        return strRetval;
    }

    public static string SaveAsText(string strfilename)
    {
        string fileName = "";
        object miss = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Document doc = null;
        try
        {
            Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
            fileName = Path.GetDirectoryName(strfilename) + @"\" + Path.GetFileNameWithoutExtension(strfilename) + ".txt";
            doc = wordApp.Documents.Open(strfilename, false);
            doc.SaveAs(fileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDOSText);

        }
        catch (Exception ex)
        {

            SendErrorMail(ex);
        }
        finally
        {
            if (doc != null)
            {
                doc.Close(ref miss, ref miss, ref miss);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(doc);
                doc = null;
            }
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        return fileName;
    }

【问题讨论】:

  • 对于 *.docx 你可以使用 OpenXML,对于 *.doc 我不知道
  • 我有一个链接,但不确定它是否适用于不同版本的所有文档文件stackoverflow.com/questions/6464757/…
  • 我正在编写一个快速概念验证应用程序;不担心使用单词自动化的性能,这立即奏效了。谢谢!
  • 是否有任何免费的好文档查看器,我可以在我的 win 应用程序中使用它来显示 doc 或 docx 文件的内容。

标签: c# ms-word


【解决方案1】:

【讨论】:

  • 是否有任何免费的好文档查看器,我可以在我的 win 应用程序中使用它来显示 doc 或 docx 文件的内容。
【解决方案2】:

Microsoft Interop Word Nuget

            string docPath = @"C:\whereEverTheFileIs.doc";
            Application app = new Application();
            Document doc = app.Documents.Open(docPath);


            string words = doc.Content.Text;
            doc.Close();
            app.Quit();

【讨论】:

    猜你喜欢
    • 2011-10-06
    • 1970-01-01
    • 2013-10-30
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    相关资源
    最近更新 更多