【发布时间】:2010-10-26 05:15:14
【问题描述】:
我很清楚 Microsoft 支持基础文章指出不支持自动化办公产品 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();
【问题讨论】:
-
仅供参考:相同的代码就像部署到 Windows Server 2003 R2 x64 和 Excel 2007 的魅力一样工作。
-
-1 帮助新读者从服务器进程尝试 Office 自动化。
-
@约翰桑德斯。好吧,以下要求该怎么做:在 xls 文件中运行“更新”宏,无需自动化?只是好奇(并不是说这样的设计一开始就是一个好主意,但用户就是用户......)
-
@Vinzz:如果有任何其他无法满足的要求,你会怎么做:说实话。
-
@John Saunder:唉,在这种特殊情况下,它可以(并且已经)遇到了。但事实上,试图说服另一种解决方案是一个更好的主意,这是正确的做法。
标签: com excel-2007 office-automation