基于Windows Server 2008 x64的Excel 2007自动化

Chr*_*ner 25 com excel-2007 office-automation

我很清楚微软的支持基础文章说它不支持自动化办公产品UI.似乎Windows Server 2008 x64和Excel 2007强制执行给定的语句.

我在NT服务(本地系统帐户)OnStart方法中运行以下代码.当它在控制台应用程序中运行相同的代码时,它所做的只是Excel自动化.

提供的代码有两部分.第一部分启动Excel,创建一个新的工作簿并将其保存到给定的文件名.第二部分启动Excel的新实例并打开给定文件.打开操作以此异常结束:

服务无法启动.System.Runtime.InteropServices.COMException(0x800A03EC):Microsoft Office Excel无法访问文件'c:\ temp\test.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();
Run Code Online (Sandbox Code Playgroud)

Chr*_*ner 31

解决方案非常简单.可以在这里找到msdn论坛帖子

总而言之,我在这里发布解决方案,归功于H Ogawa

这个解决方案是......

·Windows 2008 Server x64

请制作此文件夹.

C:\ WINDOWS\Syswow64资料\ CONFIG\systemprofile \桌面

·Windows 2008 Server x86

请制作此文件夹.

C:\ WINDOWS\system32 \设置\ systemprofile \桌面

...而不是dcomcnfg.exe.

此操作消除了我系统中的办公自动化问题.

在systemprofile文件夹中似乎需要一个Desktop文件夹来通过Excel打开文件.

它从Windows2008中消失,Windows2003有该文件夹,我认为它会导致此错误.

  • 哇,就是这样吗?我也遇到了这个问题,我认为在会话0中运行Excel是可行的.我使用.Net远程处理在登录会话1的用户下运行Excel ... (2认同)
  • +1是最简单的解决方案,也是最神奇的错误. (2认同)

Jon*_*onx 7

同样如源中所述,您需要为Desktop文件夹设置正确的权限.这适用于Windows 2008-64bits和Office 2010 32位.

  1. 创建目录"C:\ Windows\SysWOW64\config\systemprofile\Desktop"(对于64位Windows)或"C:\ Windows\System32\config\systemprofile\Desktop"(对于32位Windows)

  2. 为创建的文件夹分配用户"网络服务(服务Réseau)"以下权限:

读取和执行,列出文件夹内容,读取

约翰.