Windows Server 2008 x64 之上的 Excel 2007 自动化

发布于 2024-07-19 19:07:38 字数 2121 浏览 17 评论 0原文

我很清楚 Microsoft 支持库文章指出不支持自动化 Office 产品 UI less。 看来 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();

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.

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.

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:

Service cannot be started. System.Runtime.InteropServices.COMException (0x800A03EC): Microsoft Office Excel cannot access the file 'c:\temp\test.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.

Why was the automated excel able to launch and write files to disk but fails when it’s asked “just “ to open an existing file?

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();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

北座城市 2024-07-26 19:07:38

解决方案非常简单。 可以找到 msdn 论坛主题 这里

长话短说,我在这里发布解决方案,功劳归于H Okawa

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

・Windows 2008 Server x64

请创建此文件夹。

C:\Windows\SysWOW64\config\systemprofile\Desktop

・Windows 2008 Server x86

请创建此文件夹。

C:\Windows\System32\config\systemprofile\Desktop

...而不是 dcomcnfg.exe。

这个操作解决了我系统中的办公自动化问题。

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

它从Windows2008中消失,Windows2003有该文件夹,
我认为它导致了这个错误。

The solution is really simple. The msdn forum thread can be found here

To make a long story short I'm posting the solution here, credit goes to H Ogawa

This solution is ...

・Windows 2008 Server x64

Please make this folder.

C:\Windows\SysWOW64\config\systemprofile\Desktop

・Windows 2008 Server x86

Please make this folder.

C:\Windows\System32\config\systemprofile\Desktop

...instead of dcomcnfg.exe.

This operation took away office automation problems in my system.

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

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

屋顶上的小猫咪 2024-07-26 19:07:38

同样如源中所述,您需要为桌面文件夹设置正确的权限。
这对我在 Windows 2008-64 位和 Office 2010 32 位上有效。

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

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

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

约翰。

Also like stated in the source, you need to set the correct rights for the Desktop folder.
This worked for me on Windows 2008-64bits and Office 2010 32bits.

  1. Create directory "C:\Windows\SysWOW64\config\systemprofile\Desktop " (for 64 bit Windows) or "C:\Windows\System32\config\systemprofile\Desktop " (for 32 bit Windows)

  2. Assign user "Network Services (Service Réseau)" the following rights for the created folder:

Read & Execute, List folder contents, Read

John.

把昨日还给我 2024-07-26 19:07:38

我经常发现调用 Quit() 不足以释放资源。 添加: -并将其设置为 null。

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

尝试在 Quit() 语句之间

I've quite often found that calling Quit() isn't enough to release the resources. Try adding: -

System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);

between the Quit() statement and setting it to null.

帅的被狗咬 2024-07-26 19:07:38

为了让 Excel 在 Windows Server 2007 64 位上运行,您需要解决的错误比上面提到的错误要多得多。 请参阅 我花了整整两天的时间制定的步骤

There are many more errors than the one mentioned that you'll need to work through in order to get Excel working on Windows Server 2007 64-bit. See the steps I worked out after working on this for two full days!

你如我软肋 2024-07-26 19:07:38

如果您使用的是 Apache,您可能还需要按照以下步骤使 MS Word 正常工作(以及其他答案中概述的所有内容):

下面的屏幕截图显示了您需要打开的两个对话框:
在此处输入图像描述

对于 Apache:

服务 -> Apache -> 右键单击​​(属性)-> 登录选项卡

MS Word:

启动 dcomcnfg.exe->控制台根目录->组件服务->计算机->我的电脑->DCOM Config->查找 Microsoft 应用程序->右键单击(属性)->身份选项卡

**如果找不到 MS Word,请确保根据您安装的 Office 版本启动正确的 DCOM 配置(64 位与 32 位)。

这里有两个选项,您可以将 Apache 设置为使用本地系统帐户并选中复选框以允许桌面交互。 如果您这样做,则需要将 MS Word 的身份设置为交互式用户

否则,您需要将两者设置为同一用户(最好是登录的用户),如图所示。

If you are using Apache, you might also need to follow these steps to get MS Word working properly (along with everything outlined in other answers):

Below is a screenshot showing the two dialogs you'll need to bring up:
enter image description here

For Apache:

Services->Apache->Right Click (Properties)->Log On Tab

MS Word:

Launch dcomcnfg.exe->Console Root->Component Services->Computers->My Computer->DCOM Config->Find Microsoft Application->Right Click(Properties)->Identity Tab

**if you can't find the MS Word, make sure you are launching the correct DCOM Config (64 bit vs 32 bit) depending on what version of Office you have installed.

There are two options here, you can set Apache to use Local System Account and check the checkbox to ALLOW desktop interaction. If you do this then you need to set the Identity for MS Word to Interactive User.

Otherwise, you need to set both to the same user (Ideally the user that is logged in) like shown in the picture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文