我知道微软支持文档中指出不支持无界面自动化 Office 产品。似乎Windows Server 2008 x64 和Excel 2007执行了这个规定。
我在一个NT服务(本地系统帐户)的OnStart方法中运行以下代码。它所做的就是通过Excel自动化方式工作,就像在Console应用程序中运行相同的代码一样。
提供的代码有两部分。第一部分启动Excel,创建一个新工作簿并将其保存到给定文件名中。第二部分启动一个新的Excel实例并打开给定的文件。打开操作以此异常结束:
“服务无法启动。System.Runtime.InteropServices.COMException (0x800A03EC):Microsoft Office Excel无法访问文件'c:\temp\test.xls'。有几个可能的原因:”
• 文件名或路径不存在。 • 文件正在被另一个程序使用。 • 您要保存的工作簿与当前打开的工作簿具有相同的名称。 为什么自动化的Excel能够启动并将文件写入磁盘,但在被要求“只是”打开现有文件时失败了?
我在一个NT服务(本地系统帐户)的OnStart方法中运行以下代码。它所做的就是通过Excel自动化方式工作,就像在Console应用程序中运行相同的代码一样。
提供的代码有两部分。第一部分启动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();