C# Com OLE 服务器

发布于 2024-12-06 01:24:12 字数 782 浏览 1 评论 0原文

我试图找出使用 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 所在的位置,但我在早期绑定中看到过一些情况,您可以直接从对象调用该函数,而无需通过“InvokeMember”

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

谢谢。

I am trying to figure out the best way to interact with an OLE Server using C# .NET

i have found some code that enables interaction with COM+ that appears to work for the OLE server but I wonder if there is a more elegant or simpler way?

I require that it be late bound.

Code (as pilfered from elsewhere on the net)

// 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);

Obviously in my case I am putting the name of my ole server in where Excel.Application is, but I have seen cases in EARLY binding where you can call the function directly off the object without having to go via 'InvokeMember'

Is this possible? Can I use Type to cast as object as my type?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-12-13 01:24:12

如果您使用的是 .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;

If you are using .NET 4.0 you can use dynamic instead of object and invoke the members as if they were there. This will then be checked at runtime, and if the name is correct, execute it.

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

//Create instance of excel  
dynamic excelObject = Activator.CreateInstance(excel); 
excelObject.Visible = true;
零時差 2024-12-13 01:24:12

尝试从添加参考文献中查看这一点。它使您可以轻松访问 Excel。
微软Office核心
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;

Try having a look at this from the add references. It gives you useful access to Excel.
Microsoft.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;
}

Cells and worksheets etc can be accessed like this:

Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Item[1];
worksheet.Cells.Item[6, 1]).Value;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文