基于 Windows Server 2008 x64 的 Excel 2007 自动化 [英] Excel 2007 automation on top of a Windows Server 2008 x64

查看:12
本文介绍了基于 Windows Server 2008 x64 的 Excel 2007 自动化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I’m well aware of the Microsoft support base article stating that it’s not supported to automate office products UI less. It seems that Windows Server 2008 x64 and Excel 2007 enforce the given statement.

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

I’m running the following code in a NT Service (Local System account) OnStart method. All it does is Excel automation the way it’s working when you run the same code in a Console Application.

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

The provided code has two parts. The first part launches Excel, creates a new work book and saves it to the given filename. The second part launches a new instance of Excel and opens the given file. The open operation ends in this exception:

服务无法启动.System.Runtime.InteropServices.COMException (0x800A03EC):Microsoft Office Excel 无法访问文件c: emp est.xls".有几个可能的原因:

Service cannot be started. System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'c: emp est.xls'. There are several possible reasons:

• 文件名或路径不存在.• 该文件正被另一个程序使用.• 您尝试保存的工作簿与当前打开的工作簿同名.

• The file name or path does not exist. • The file is being used by another program. • The workbook you are trying to save has the same name as a currently open workbook.

为什么自动 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 Ogawa

这个解决方案是...

・Windows 2008 Server x64

・Windows 2008 Server x64

请制作这个文件夹.

C:WindowsSysWOW64configsystemprofileDesktop

C:WindowsSysWOW64configsystemprofileDesktop

・Windows 2008 Server x86

・Windows 2008 Server x86

请制作这个文件夹.

C:WindowsSystem32configsystemprofileDesktop

C:WindowsSystem32configsystemprofileDesktop

...而不是 dcomcnfg.exe.

...instead of dcomcnfg.exe.

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

This operation took away office automation problems in my system.

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

A Desktop folder seems to be necessary in the systemprofile folder to open file by Excel.

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

It disappears from Windows2008, Windows2003 had the folder, and I think it cause this error.

这篇关于基于 Windows Server 2008 x64 的 Excel 2007 自动化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆