【问题标题】:How to run Interop.Excel.Application with command arguments?如何使用命令参数运行 Interop.Excel.Application?
【发布时间】:2020-10-23 11:55:45
【问题描述】:

我需要将参数传递给新的 excel 实例,然后通过 Addin 从新实例中读取参数。

我想用这个结构打开一个新的excel应用:

new Microsoft.Office.Interop.Excel.Application()

有没有办法传递命令行参数,例如:

string argument = "TestArgument";
new Microsoft.Office.Interop.Excel.Application(argument);

谢谢!

【问题讨论】:

    标签: c# office-interop office-addins excel-interop excel-addins


    【解决方案1】:

    如果你必须这样做(我建议尽可能快地从 Interop 运行)你 可以启动 Excel 应用程序,然后获取对它的引用。

    // start Excel with your command line arguments
    var pathToExcel = @"C:\Program Files\Microsoft Office\root\Office16\excel.exe";
    var commandLineArguments = "/whatever";
    System.Diagnostics.Process.Start(pathToExcel, commandLineArguments);
    
    // get a reference to it
    Microsoft.Office.Interop.Excel.Application app = (Microsoft.Office.Interop.Excel.Application)
        System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
    

    互操作是如此邪恶,以至于我觉得回答这个问题几乎是不道德的。它打开了通往无尽痛苦的大门。如果可能的话,我建议使用 EPPlus 之类的东西与 Excel 文件进行交互。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      相关资源
      最近更新 更多