c# - 无法在 .Net4.0 中使用 Process.Start() 打开 excel 文件

标签 c# .net excel process.start

最近一个.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/

相关文章:

C# 从 XML 中选择属性或元素

c# - Azure 和本地 TCP 连接数有很大不同

r - 从 Excel 导入时如何忽略隐藏数据

c# - 可以使用 Assert 作为前置条件吗?

javascript - 带有 ExtJS 的 ASP.NET MVC 3 返回错误的 json

.net - SqlDataReader 抛出 NullReferenceException!什么可能导致此问题以及如何调试?

c# - DotNetZip 卡住我的应用程序?

python - 使用 "ws.cell(row = row, column = columnl).value = looprow"删除之前保存在 python 中的数据

EXCEL:查找和替换,更改整个单元格值

c# - 在基础设施层使用依赖注入(inject)