Microsoft.Office.Interop 用于网站(文件转换)是否安全? [英] Is Microsoft.Office.Interop safe to use for (file-converting) for a website?

查看:20
本文介绍了Microsoft.Office.Interop 用于网站(文件转换)是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个关于女巫用户必须添加报告(Word 文档)并能够查看它们的网站,我将 *.doc 转换为 *.pdf,然后通过 pdf.js 显示它们.对于转换,我使用 Microsoft.Office.Interop.Word.代码看起来像

I am coding a website on witch users have to add reports (Word document's) and to make possible to view them I convert *.doc to *.pdf, and then displaying them throught pdf.js. For converting i use Microsoft.Office.Interop.Word. Code looks like

public void ConvertDocument(string PATH)
    {
        FileInfo FILE = new FileInfo(PATH);

        if (FILE.Extension.ToLower() == ".doc" || FILE.Extension.ToLower() == ".docx" || FILE.Extension.ToLower() == ".docm" || FILE.Extension.ToLower() == ".dotx" || FILE.Extension.ToLower() == ".dotm")
        {
            if (FILE.Length == 0)
            {
                return;
            }

            object oMissing = System.Reflection.Missing.Value;
            Word.Application word = new Word.Application();

            try
            {
                word.Visible = false;
                word.ScreenUpdating = false;

                Object filename = (Object)FILE.FullName;
                Word.Document doc = word.Documents.Open(ref filename, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                try
                {
                    doc.Activate();
                    object outputFileName = FILE.FullName.Replace(FILE.Extension, ".PDF");
                    doc.SaveAs(ref outputFileName, Word.WdSaveFormat.wdFormatPDF, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                               ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                }

                finally
                {
                    object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                    ((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                    doc = null;
                }
            }

            finally
            {
                ((Word._Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                word = null;
            }

            File.Delete(PATH);
        }
}

  1. 那安全吗?
  2. 它将处理多少用户?
  3. 它需要什么资源?
  4. 我应该在服务器上安装 MS Office 来运行网站吗?
  5. 这真的是一个好方法吗?

推荐答案

微软给出的答案是否定的:

The answer, from Microsoft, is no:

微软目前不推荐,也不支持,从任何无人值守的情况下自动化 Microsoft Office 应用程序,非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务),因为 Office 可能表现出不稳定在此环境中运行 Office 时的行为和/或死锁.

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

来自办公服务器端自动化的考虑

根据经验,以下是我们遇到的问题:

From experience, here are the issues we encountered:

  • 当进程意外存在时,会留下大量 Word 实例,并且永远不会被清理
  • Word 安装在处理会话中途关闭后损坏
  • 它的扩展性不是很好 - Word 是一个大型桌面应用程序,它的功能非常出色;但是,它并不是真正适用于您使用它的流程,因此,打开它的大量实例会消耗您的应用程序可以使用的资源.

但是,还有其他方法可以做到这一点,如此 StackOverflow 中所述问答

There are other ways to do this, however, as covered in this StackOverflow question and answers

您可以考虑预先转换word文档——例如,在上传文档时,是否可以同时创建PDF?这样,您的服务器只需提供 PDF 文档,而无需为请求提供服务.

You may consider pre-converting the word documents - for example, is it possible, when the document is uploaded, to also create the PDF then? That way, your server is simply serving up a PDF document and has to do very little work in servicing the request.

这篇关于Microsoft.Office.Interop 用于网站(文件转换)是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆