【问题标题】:C# Com OLE ServerC# Com OLE 服务器
【发布时间】:2011-11-23 12:51:29
【问题描述】:

我正在尝试找出使用 C# .NET 与 OLE 服务器交互的最佳方式

我发现了一些可以与 COM+ 交互的代码,这些代码似乎适用于 OLE 服务器,但我想知道是否有更优雅或更简单的方法?

我要求它是后期绑定的。

代码(从网上其他地方偷来的)

// Code start
Type excel;
object[] parameter = new object[1];
object excelObject;
try
{
    //Get the excel object 
    excel = Type.GetTypeFromProgID("Excel.Application");

    //Create instance of excel 
    excelObject = Activator.CreateInstance(excel);

    //Set the parameter whic u want to set 
    parameter[0] = true;


    //Set the Visible property 
    excel.InvokeMember("Visible", BindingFlags.SetProperty, null, excelObject, parameter);

显然,在我的情况下,我将我的 ole 服务器的名称放在 Excel.Application 所在的位置,但我在 EARLY 绑定中看到了一些情况,您可以直接从对象调用函数而无需通过“InvokeMember”

这可能吗?我可以使用 Type 将对象转换为我的类型吗?

谢谢。

【问题讨论】:

  • 它被称为 COM,而不是 OLE。 OLE 已经死去并被埋葬了一段时间。这将帮助您在“添加参考”对话框中找到该选项卡。
  • 嗯,显然我要引用的是 OLE 服务器,它肯定是一个 .exe 的 ole 应用程序并且是自我注册的......我知道 COM+ 是由 OLE 诞生的,但这是一个旧服务器。
  • 然后使用“添加引用”对话框中的 OLE 选项卡 :)

标签: c# .net com ole


【解决方案1】:

如果您使用的是 .NET 4.0,则可以使用 dynamic 而不是 object 并像调用成员一样调用成员。这将在运行时进行检查,如果名称正确,则执行它。

//Get the excel object  
var excel = Type.GetTypeFromProgID("Excel.Application"); 

//Create instance of excel  
dynamic excelObject = Activator.CreateInstance(excel); 
excelObject.Visible = true;

【讨论】:

    【解决方案2】:

    尝试从添加引用中查看此内容。它为您提供了对 Excel 的有用访问。 微软.Office.Core Microsoft.Office.Interop.Excel

    using Excel = Microsoft.Office.Interop.Excel;
    

    ...

    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        Excel.Application app;
        Excel.Workbook workbook;
    
        app = new Excel.ApplicationClass();
        app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;
    
        workbook = app.Workbooks.Open(  openFileDialog.FileName,
                                        0,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing,
                                        Type.Missing);
    
        return workbook;
    }
    

    可以像这样访问单元格和工作表等:

    Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Item[1];
    worksheet.Cells.Item[6, 1]).Value;
    

    【讨论】:

    • 添加引用不会使绑定提前吗? Excel 也只是另一个 ole 服务器的一个示例。
    猜你喜欢
    • 1970-01-01
    • 2017-04-15
    • 2017-08-30
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2015-06-23
    • 2014-02-19
    • 1970-01-01
    相关资源
    最近更新 更多