我在从 ASP.NET 站点打开 word 文档时遇到问题。该解决方案在 Windows 2003 Server 上运行良好,但不适用于 Windows 2008 服务器 x64 和 Windows 7 x64。
为了简化解决方案,我创建了一个 ASP.NET MVC 3 站点并尝试从那里打开 word 文档。
我的环境:Windows 7 x64 和 MS Office 2010 x64
打开文档的代码如下:
public ActionResult WordTest()
{
var fullFileName = @"C:\inetpub\wwwroot\fpub\TestDocument.docx";
var impersonation = new ImpersonationManager();
impersonation.Impersonate();
try
{
var application = new Application();
try
{
Type documentsType = application.Documents.GetType();
var document =(_Document)documentsType.InvokeMember("Open", BindingFlags.InvokeMethod, null, application.Documents,
new object[] {fullFileName});
try
{
return View(new ModelData {Result = document == null ? "Bad" : "OK"});
}
finally
{
if (document != null)
{
document.Close(false);
Marshal.ReleaseComObject(document);
}
}
}
finally
{
application.Quit(false);
Marshal.ReleaseComObject(application);
}
}
finally
{
impersonation.CloseImpersonation();
}
}
首先,我模拟使用受信任的域用户帐户进行单词交互(ImpersonationManager 是一个自定义组件)。此用户有权打开\保存\关闭 Word 应用程序。在我的测试中,这是我自己的帐户:)
然后我创建 Word 应用程序实例。 WINWORD 进程在模拟帐户下启动。
但在调用“Open”方法后,它总是返回 null。没有异常(exception),事件查看器中没有信息。
此外,在此之后 Word 进程会 100% 加载 CPU(1 个 CPU 核心)。
如果我运行与控制台应用程序相同的代码(没有模拟),它工作正常。
我想知道这里可能有什么问题?
更新 如果 Visual Studio Development 服务器用作站点的主机,它工作正常
最佳答案
MS 不支持在服务器场景(如 ASP.NET、Windows 服务等)中使用 Office 互操作 - 请参阅 http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2
此外,自 Windows Vista 以来,还有一些与安全相关的更改,这基本上使得在 Windows 服务中做任何“类似桌面”的事情变得非常困难(IIS/ASP.NET 只是 Windows 服务在这方面的一个特例) ).
有几个库(免费的和商业的)可以处理 Office 文件(没有 Office Interop)...为了进一步帮助您描述您的目标。
关于asp.net - ApplicationClass Documents.Open 为 64 位系统上的 ASP.NET 站点返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7689883/