2

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

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. 这实际上是一个好方法吗?
4

1 回答 1

5

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.

于 2013-05-27T20:39:31.193 回答