繁体   English   中英

将Word doc转换为html时,Asp.net页面挂起

[英]Asp.net page hangs when converting word doc to html

我有一个功能,可以将Word文档保存为html文件,作为基于Web的文档查看器的一部分。 它将返回新创建的html文件的路径,然后将其显示在iframe中。 我将其内容粘贴在下面以供参考。

当我从Visual Studio运行Web应用程序时,所有这些都可以正常工作,但是当它安装在IIS上时,它只是挂起了。 html文件未创建。

在这种情况之前,该函数仅能实例化单词Application class。 我收到以下错误消息:

“由于以下错误,检索组件{Guid-thing}的COM类工厂失败...”

通过转到组件服务->计算机->我的电脑-> DCOM Config解决了该问题。 我进入了Microsoft Word 97-2003 Docment的安全属性,并为IIS用户/网络服务授予了本地启动本地激活权限。 后来,我还授予了“ 远程启动”和“ 远程激活”权限。

当我尝试查看文档时,我在任务管理器中的进程下看到WINWORD.exe已由网络服务打开。 因此,它比以前更进一步。 但是现在在我看来,它要么挂在wordApp.Documents.Open上,要么挂在doc.SaveAs行上。 我已经将它拖延了十分钟,但是却没有超时或显示错误。

Web应用程序安装在Windows Server 2003 SP2的IIS 6中

private string WordToHTML(string filePath)
{
    string returnPath = String.Empty;
    Microsoft.Office.Interop.Word.Document doc = null;
    object missing = System.Reflection.Missing.Value;
    Microsoft.Office.Interop.Word.Application wordApp = null;

    try
    {
        // Initialise
        string serverFolderPath = Path.GetDirectoryName(filePath);
        string fileToOpen = filePath;
        string FileToSave = Path.GetFileNameWithoutExtension(fileToOpen) + ".html";

        // Open Word
        wordApp = new Microsoft.Office.Interop.Word.Application();

        // Get Doc
        doc = wordApp.Documents.Open(fileToOpen, ref missing, true, ref missing, 
                                     ref missing, ref missing, ref missing, 
                                     ref missing, ref missing, ref missing, 
                                     ref missing, false, ref missing, ref missing, 
                                     ref missing, ref missing);

        // If html file already exists, delete it 
        returnPath = String.Format("{0}\\{1}", serverFolderPath, FileToSave);  
        if (File.Exists(returnPath))
            File.Delete(returnPath);

        // Save as HTML document
        doc.SaveAs(returnPath, 10, ref missing, ref missing, 
                   ref missing, ref missing, ref missing, ref missing, 
                   ref missing, ref missing, ref missing, ref missing, 
                   ref missing, ref missing, ref missing, ref missing);
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        doc.Close(ref missing, ref missing, ref missing);
        wordApp.Quit(ref missing, ref missing, ref missing);
    }
    return returnPath;
}

从代码内使用任何MS Office产品极容易出现这种问题,并且(尽管可能会出现建议),您确实应该不惜一切代价避免执行此处的操作。

您很可能会遇到“无提示的ui提示”。 屏幕上应该有一个UI提示来响应,但是由于执行上下文/和您没有登录到所讨论的计算机这一事实,它永远不会出现。

使用这种形式的MS Office东西时,其他注意事项还包括处理对象时的问题。 如果您不能达到100%完美,那么您的应用程序中将发生大量内存泄漏,随着时间的推移,内存泄漏将不断降低。 我发现,如果您编写的代码从不将调用链接在一起,并且始终在将每个对象分配给变量之前使用它,则实现起来会稍微容易一些(因为您可以专门处理每个对象

您还应该查看将文档格式从97/2003升级到Office 2007及更高版本的xml格式。 文件大小明显更好,并且与可能会通过网络为您查看的最新产品更加兼容。

您应该问(正确,可靠地)完成这项工作的成本是否比仅使用Microsoft自己的“ Office Web Apps”便宜,您可以通过企业内部的SharePoint或竞争性的成本通过Office 365来访问它并为您提供了“完美的网页浏览”功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM