c# - 如何从 MS-Word 文档文件中提取文本数据

标签 c# ms-word

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

同时指导我是否有任何可用的查看器,我可以使用它来显示来 self 的 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;
    }

最佳答案

关于c# - 如何从 MS-Word 文档文件中提取文本数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14851133/

相关文章:

c# - 使用属性名称的字符串表示访问类属性和方法

c# - 是否有一些正确的方法可以为 IEnumerable 模型字段添加模型错误以突出显示 View 中的特定输入但不是全部?

c# - 添加新包会破坏 .NET 5 应用程序

c# - 防止访问已处置的引用

c# - 获取当前列名

c# - 通过 .NET 自动化在 Word 文档中搜索和替换

php - 使用 PHP 转换 Microsoft Word 特殊字符

ms-word - Microsoft Word 中的两列布局交换

c# - 如何从 Word 文档中获取主题和标题(无需打开)?

ms-word - 计算两个日期之间的天数(以ms word为单位)