Windows Server 2008 x64 之上的 Excel 2007 自动化
-
21-08-2019 - |
题
我很清楚 Microsoft 支持库文章指出不支持自动化办公产品 UI less。看起来 Windows Server 2008 x64 和 Excel 2007 执行给定的声明。
我在 NT 服务(本地系统帐户)OnStart 方法中运行以下代码。它所做的只是 Excel 自动化,就像您在控制台应用程序中运行相同代码时的工作方式一样。
提供的代码有两部分。第一部分启动 Excel,创建一个新工作簿并将其保存到给定的文件名。第二部分启动 Excel 的新实例并打开给定的文件。打开操作在此异常中结束:
服务无法启动。System.Runtime.InteropServices.COMException(0x800A03EC):Microsoft Office Excel 无法访问文件“c: emp est.xls”。可能的原因有以下几种:
• 文件名或路径不存在。• 该文件正在被另一个程序使用。• 您尝试保存的工作簿与当前打开的工作簿同名。
为什么自动 Excel 能够启动文件并将文件写入磁盘,但当要求“仅”打开现有文件时却失败?
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
// launch excel and create/save a new work book
Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
excel.UserLibraryPath, excel.Interactive));
//
string filename = "c:\\temp\\test.xls";
if(System.IO.File.Exists(filename)) System.IO.File.Delete(filename);
//
excel.Workbooks.Add(System.Reflection.Missing.Value);
excel.Save(filename);
excel.Quit();
excel = null;
// lauch new instance of excel and open saved file
excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
try
{
Microsoft.Office.Interop.Excel.Workbook book = excel.Workbooks.Open(filename,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
true,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value,
false,
false,
System.Reflection.Missing.Value,
false,
System.Reflection.Missing.Value,
System.Reflection.Missing.Value);
book.Close(false, System.Reflection.Missing.Value, System.Reflection.Missing.Value);
book = null;
}
finally
{
excel.Quit();
excel = null;
}
//
GC.Collect();
解决方案
解决方案非常简单。msdn论坛帖子可以找到 这里
长话短说,我在这里发布解决方案,功劳归于 H小川
这个解决方案是...
・Windows 2008 服务器 x64
请制作这个文件夹。
C:\Windows\SysWOW64\config\systemprofile\Desktop
・Windows 2008 服务器 x86
请制作这个文件夹。
C:\Windows\System32\config\systemprofile\Desktop
...而不是 dcomcnfg.exe。
这个操作解决了我系统中的办公自动化问题。
systemprofile 文件夹中似乎需要一个 Desktop 文件夹才能通过 Excel 打开文件。
它从Windows2008中消失, Windows2003具有文件夹, 我认为这是导致这个错误的原因。
其他提示
同样如源中所述,您需要为桌面文件夹设置正确的权限。这对我在 Windows 2008-64 位和 Office 2010 32 位上有效。
创建目录“C:\Windows\SysWOW64\config\systemprofile\Desktop”(对于 64 位 Windows)或“C:\Windows\System32\config\systemprofile\Desktop”(对于 32 位 Windows)
为用户“网络服务(Service Réseau)”分配所创建文件夹的以下权限:
读取和执行、列出文件夹内容、读取
约翰。
我经常发现调用 Quit() 不足以释放资源。尝试添加:-
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
在 Quit() 语句之间并将其设置为 null。
为了让 Excel 在 Windows Server 2007 64 位上运行,您需要解决的错误比上面提到的错误要多得多。看 我制定的步骤 经过整整两天的努力!
如果您使用 Apache,您可能还需要按照以下步骤使 MS Word 正常工作(以及其他答案中概述的所有内容):
下面的屏幕截图显示了您需要打开的两个对话框:
对于阿帕奇:
服务->Apache->右键单击(属性)->登录选项卡
微软Word:
启动 dcomcnfg.exe->控制台根目录->组件服务->计算机->我的电脑->DCOM 配置->查找 Microsoft 应用程序->右键单击(属性)->身份选项卡
**如果找不到 MS Word,请确保根据您安装的 Office 版本启动正确的 DCOM 配置(64 位与 32 位)。
这里有两个选项,你可以设置Apache使用 本地系统帐户 并选中复选框以允许桌面交互。如果您这样做,那么您需要设置 身份 对于 MS Word 来说 互动用户.
否则,您需要将两者设置为同一用户(最好是登录的用户),如图所示。