最近一个.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文件的路径,比如“C:\project\test.xls” excel 文件打开时,其中的宏将立即运行。
在 .Net1.1 中,代码和宏都运行良好。在 .Net4 中,代码无一异常(exception)地运行。但是宏不起作用。
为什么excel文件中的宏没有运行?
我的环境是Win7 64位。办公室 2010 64 位。 IIS 7.5
谢谢
当我更改为以下代码并运行 Debug模式时
process.StartInfo.FileName = marco_file;
if (process.Start())
{
Debug.WriteLine("-->start ");
}
else
{
Debug.WriteLine("-->failed ");
}
结果是进入了else block ,也就是进程启动失败。
谁知道为什么?
最佳答案
检查 Excel 中的安全性。默认情况下,当您打开包含宏的工作簿时,Excel 2010 将禁用宏,您必须明确启用它们。
要启用宏,您需要转到:
File tab on the ribbon => Options
=> Trust Center
=> Trust Center Settings...
=> Macro Settings
这里唯一允许宏自动运行的选项是:
Enable all macros (not recommended; potentially dangerous code can run)
不过请注意警告。
关于c# - 无法在 .Net4.0 中使用 Process.Start() 打开 excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9780820/