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



我正在为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;
}

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

由于以下

错误,检索具有 CLSID {000209FF-0000-0000-C000-0000000000046} 的组件的 COM 类工厂失败:80070005访问被拒绝。(HRESULT的例外:0x80070005(E_ACCESSDENIED((。

我也尝试了此链接上提供的步骤 https://learn.microsoft.com/en-us/windows/win32/wmisdk/securing-a-remote-wmi-connection?redirectedfrom=MSDN 但仍然出现相同的结果,如果有人指导我,这将是非常感激的行为。

请改用 Open XML SDK。

"Office 服务器端自动化的注意事项"页指出以下内容:

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

如果要生成在服务器端上下文中运行的解决方案,则应尝试使用已安全执行无人参与的组件。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方法。如果使用服务器端解决方案中的 Office 应用程序,则该应用程序将缺少成功运行所需的许多功能。此外,您将承担整体解决方案稳定性的风险。

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

最新更新