简体   繁体   English

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

[英]Fail to open excel file using Process.Start() in .Net4.0

Recently an old project of .Net 1.1 is migrating to .Net4.0. 最近,.Net 1.1的一个旧项目正在迁移到.Net4.0。 The project is using the following method to open excel file: 该项目使用以下方法打开excel文件:

Process process = new Process();

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

And the marco_file is the path of excel file with macros, like "C:\\project\\test.xls" The macro in it will run immediately when the excel file open. 而marco_file是带有宏的excel文件的路径,如“C:\\ project \\ test.xls”。当excel文件打开时,它中的宏将立即运行。

In .Net1.1 both the codes and the macro runs well. 在.Net1.1中,代码和宏都运行良好。 In .Net4, the codes runs without exceptions. 在.Net4中,代码无异常运行。 But the macro didn't work. 但宏观无效。

Why the macro in the excel file didn't run? 为什么excel文件中的宏没有运行?

My environment is Win7 64bit. 我的环境是Win7 64bit。 Office 2010 64bit. Office 2010 64bit。 IIS 7.5 IIS 7.5

Thanks 谢谢

When I change to the following codes and run the debug mode 当我更改为以下代码并运行调试模式时

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

The result is that it goes into the else block, which means the process fails to start. 结果是它进入了else块,这意味着进程无法启动。

Any one knows why? 谁知道为什么?

Check the security in Excel. 检查Excel中的安全性。 By default Excel 2010 will disable Macros when you open a workbook containing them, and you have to explicitly enable them. 默认情况下,Excel 2010将在您打开包含它们的工作簿时禁用宏,并且您必须明确启用它们。

To enable macros you need to go to: 要启用宏,您需要转到:

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

The only option here that will allow the macro to run automatically is: 这里唯一允许宏自动运行的选项是:

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

Note the warning, though. 但请注意警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM