无法使用.Net4.0中的Process.Start()打开Excel文件

最近,.NET 1.1的一个旧项目正在迁移到.Net4.0。 该项目使用以下方法打开excel文件:

Process process = new Process(); try { process.StartInfo.FileName = marco_file; process.Start(); } catch(Exception) { } finally { process.Close(); } 

而marco_file是excel文件的macros的path,如“C:\ project \ test.xls”,其中的macros将在Excel文件打开时立即运行。

在.Net1.1中,代码和macros都运行良好。 在.Net4中,代码无一例外地运行。 但是macros不起作用。

为什么excel文件中的macros没有运行?

我的环境是Win7 64bit。 Office 2010 64bit。 IIS 7.5

谢谢

当我更改为以下代码并运行debugging模式

 process.StartInfo.FileName = marco_file; if (process.Start()) { Debug.WriteLine("-->start "); } else { Debug.WriteLine("-->failed "); } 

结果是它进入了else块,这意味着进程无法启动。

任何人都知道为什么?

检查Excel中的安全性。 默认情况下,Excel 2010将在打开包含它们的工作簿时禁用macros,并且必须明确启用它们。

要启用macros,你需要去:

 File tab on the ribbon => Options => Trust Center => Trust Center Settings... => Macro Settings 

这里唯一允许macros自动运行的选项是:

 Enable all macros (not recommended; potentially dangerous code can run) 

注意警告,但。