无法使用.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)
注意警告,但。
- 在使用Microsoft.Ace.Oledb.12.0提供程序时,数据在255个字节后被截断
- 我如何使用Parallel.ForEach for Excel?
- excel 2010和2013的数据模型//数据检索
- 更新excel文件中的小计
- Excel / EPPlus数字格式是否与DateTimeFormat.ShortDatePattern兼容?
- Excel插入行(不是添加)
- VBA运行时错误:'459'使用自定义ActiveX控件运行userform时发生外部exception
- 当Excel 2007以兼容模式打开XLS文件时,FileSystemWatcher.Changed立即触发
- C#.NET Excel问题。 应用程序在Excel中处理.txt文件并上传到SharePoint