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

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

问题描述

我编码在巫用户的网站已经加入报告(Word文档的),并做出可以查看他们,我转换的* .doc为* .PDF,然后显示它们throught pdf.js.对于转换我使用的Microsoft.Office.Interop.Word。 code看起来像

 公共无效ConvertDocument(字符串PATH)
    {
        FileInfo的FILE =新的FileInfo(PATH);        如果(FILE.Extension.ToLower()==名为.doc|| FILE.Extension.ToLower()==的.docx|| FILE.Extension.ToLower()==.DOCM|| FILE.Extension .ToLower()==.DOTX|| FILE.Extension.ToLower()==.dotm)
        {
            如果(FILE.Length == 0)
            {
                返回;
            }            反对oMissing = System.Reflection.Missing.Value;
            Word.Application字=新Word.Application();            尝试
            {
                word.Visible = FALSE;
                word.ScreenUpdating = FALSE;                目标文件名=(对象)FILE.FullName;
                Word.Document DOC = word.Documents.Open(REF文件名,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,REF oMissing, REF oMissing,裁判oMissing,裁判oMissing);
                尝试
                {
                    doc.Activate();
                    对象outputFileName = FILE.FullName.Replace(FILE.Extension.PDF);
                    doc.SaveAs(REF outputFileName,Word.WdSaveFor​​mat.wdFormatPDF,裁判oMissing,REF oMissing,
                               REF oMissing,裁判oMissing,裁判oMissing,REF oMissing,
                               REF oMissing,裁判oMissing,裁判oMissing,REF oMissing,
                               REF oMissing,裁判oMissing,裁判oMissing,裁判oMissing);
                }                最后
                {
                    对象的SaveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
                    ((Word._Document)DOC).Close(REF的SaveChanges,裁判oMissing,裁判oMissing);
                    DOC = NULL;
                }
            }            最后
            {
                ((Word._Application)字).Quit(REF oMissing,裁判oMissing,裁判oMissing);
                字= NULL;
            }            File.Delete(PATH);
        }
}


  1. 是安全的吗?

  2. 有多少用户将它处理?

  3. 哪些资源需要?

  4. 我应该在服务器上安装微软Office运行的网站?

  5. 是实际上是一个很好的方式做到这一点?


解决方案

答案,来自微软,是否定的:


  

微软目前并不提倡,不支持,
  从任何无人看管的Microsoft Office应用程序的自动化,
  非交互式客户端应用程序或组件(包括ASP,
  ASP.NET,DCOM和NT服务),因为Office可能会出现不稳定
  当Office在这种环境中运行的行为和/或死锁。


注意事项的Office服务器端自动化

根据经验,这里是我们遇到的问题:


  • 当进程意外存在,大量的Word实例是左左右,而从不清理

  • 在Word安装成了腐败后通过处理会话发生了关机中途

  • 它没有规模非常好 - 字是一个大的桌面应用程序,尤其擅长做什么的;然而,这不是真的意味着你正在使用它,并且,正因为如此,很多开放它的实例会消耗你的应用程序可以使用的资源的过程。

,还有其他方法可以做到这一点,但是作为涵盖这个计算器问题和答案

您可以考虑pre转换的Word文档 - 例如,是否有可能,文档被上传时,也创建PDF呢?这样一来,你的服务器只是提供了一个PDF文档,并需要做很少的工作在服务的要求。

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. Is that safe?
  2. And how many users will it handle?
  3. What resources it needs?
  4. Should I install MS Office on the server to run the website?
  5. Is that actually a good way to do that?

解决方案

The answer, from Microsoft, is no:

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 Considerations for Server-Side Automation of Office

From experience, here are the issues we encountered:

  • When the process exists unexpectedly, lots of Word instances are left around, and are never cleaned up
  • The Word installation became corrupt after a shutdown occurred half-way through a processing session
  • It doesn't scale remarkably well - Word is a large desktop application that is excellent at what it does; however, it's not really meant for the process you are using it for, and, as such, opening lots of instances of it will consume resources that your application could use.

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

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天全站免登陆