Is Microsoft.Office.Interop safe to use for (file-

2019-02-26 00:35发布

问题:

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?

回答1:

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.