【问题标题】:How to read a .doc and .docx file in Asp.Net MVC3 and display in TextBox如何在 Asp.Net MVC3 中读取 .doc 和 .docx 文件并在 TextBox 中显示
【发布时间】:2012-01-20 10:03:20
【问题描述】:

我有一个带有浏览和提交按钮的视图。当用户单击浏览时,可以浏览 .doc 或 .docx 文件,并且当单击提交按钮时,所选文件的文本应填充到同一视图的文本框中. 下面是我在 TextBox 中读取和显示文本的代码。

            string filePath =null,docText=null;
            foreach (string inputTagName in Request.Files)
            {
                HttpPostedFileBase Infile = Request.Files[inputTagName];
                if (Infile.ContentLength > 0 && (Path.GetExtension(Infile.FileName) == ".doc"))
                {
                    filePath = Path.Combine(
                    AppDomain.CurrentDomain.BaseDirectory,
                    Path.GetFileName(Infile.FileName));
                    if (System.IO.File.Exists(filePath))
                    {
                        System.IO.File.Delete(filePath);
                    }
                    Infile.SaveAs(filePath);
                }
                if (filePath != null)
                {

                    docText = System.IO.File.ReadAllText(filePath);

                }
                ViewBag.displayTextInTextBox= docText;
            }
            return View();

下面是我的查看代码

<input type="text" id="test1" name="test" value="@ViewBag.displayTextInTextBox">

它显示特殊字符(如 ��ࡱ�)而不是 .doc/.docx 文档中的文本。 我是在错误地读取文件还是我的代码有什么问题。

【问题讨论】:

    标签: asp.net-mvc-3


    【解决方案1】:

    我会考虑使用 OpenXML SDK 从 word 文档中提取信息,而不是使用 Word 自动化,这需要在您的服务器上安装 Word(这可能不是一个好主意):

    http://www.microsoft.com/download/en/details.aspx?id=5124

    请注意,这不适用于 .doc 文件,仅适用于 docx。

    【讨论】:

      【解决方案2】:

      Sam 你可以看看我的问题here,我之前也问过,如果你觉得它有用的话。 实际上,对于这类问题,您需要自己探索类并根据您的需要使用它。 这个答案将为您提供您必须做的基础知识。

      【讨论】:

        【解决方案3】:

        非常感谢朋友们的帮助。 以下是我所做的,它解决了这个问题,

        Microsoft.Office.Interop.Word.ApplicationClass wordApp = new 
        
            Microsoft.Office.Interop.Word.ApplicationClass();
                            string filePath1 = filePath;
                            object file = filePath1;
                            object nullobj = System.Reflection.Missing.Value;
        
           Microsoft.Office.Interop.Word.Document doc = wordApp.Documents.Open(ref file,
                         ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                         ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj,
                                                                         ref nullobj);
                         Microsoft.Office.Interop.Word.Document doc1 = wordApp.ActiveDocument;
                         string m_Content = doc1.Content.Text;
                         ViewBag.test = m_Content;
                         doc.Close(ref nullobj, ref nullobj, ref nullobj);
        

        我正在使用 MSWord 的 COM 对象。 希望这是最好的方法。

        【讨论】:

        • 如前所述,完全不推荐这样做。 support.microsoft.com/kb/257757Microsoft 目前不推荐也不支持任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office 可能表现出不稳定Office 在此环境中运行时的行为和/或死锁。
        猜你喜欢
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-15
        • 1970-01-01
        • 2012-07-26
        相关资源
        最近更新 更多