使用 microsoft.interope.word 从 ASP.Net 应用程序访问 Office 被拒绝

Access denied to Office from ASP.Net application using microsoft.interope.word

我在 ERP 系统工作,我们将文件发送给我们的办公室员工以对文件进行数字签名,为此我们想要阅读 .docx 文件,并使用 Microsoft.Office.Interop.Word 参考。

以下是我试过的代码:

            string allText = "";
            // if word file
            if (fileExtension == ".docx" || fileExtension == ".doc")
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
                stopwatch.Stop();
                object miss = System.Reflection.Missing.Value;
                object readOnly = true;
                object Path = filePath;
                // getting the document
                Microsoft.Office.Interop.Word.Document doc = word.Documents.Open(ref Path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

                // if document contains at least single line of signature
                bool docHasSignature = doc.Content.Find.Execute("{{signature_");
                if (docHasSignature)
                {
                    // reading all the and appending in the string
                    allText += (doc.Content.Text.ToString());
                }

                //signatureCount = 3;
            }

当我尝试从用户端 运行 此代码并请求此方法时,它给出以下错误:

Retrieving the COM class factory for component with CLSID {000209FF-0000-0000-C000-000000000046} failed due to the following error: 80070005 Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).

我也试过这个link提供的步骤 https://docs.microsoft.com/en-us/windows/win32/wmisdk/securing-a-remote-wmi-connection?redirectedfrom=MSDN还是出现同样的结果,如果有人指导我,将不胜感激。

改用Open XML SDK

Considerations for server-side Automation of 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.

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.

本文介绍了使用 Office 自动化的可能替代方法。