【问题标题】:Fail to open excel file using Process.Start() in .Net4.0在 .Net4.0 中使用 Process.Start() 无法打开 excel 文件
【发布时间】:2012-03-20 03:01:48
【问题描述】:

最近 .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 中,代码无异常运行。但是宏不起作用。

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

我的环境是Win7 64bit。办公室 2010 64 位。 IIS 7.5

谢谢

当我更改为以下代码并运行调试模式时

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

结果是进入else块,表示进程启动失败。

有人知道为什么吗?

【问题讨论】:

  • 你确定你的代码运行没有任何异常吗?你好像在吃它们。
  • 去掉catch{}再试一次,看看有没有抛出异常以及是什么异常。
  • 试着把它写成 catch(Exception ex) { // print exception } 而不是 catch(Exception)
  • 问题解决了!

标签: c# .net excel process.start


【解决方案1】:

检查 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)

但请注意警告。

【讨论】:

  • 安全设置最低。启用所有宏
  • 直接打开文件宏会自动运行吗?
  • 是的。如果我打开它会自动运行
  • 您的代码是打开文件但不运行宏,还是根本不打开文件?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
相关资源
最近更新 更多