C# Com OLE 服务器
我试图找出使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用的是 .NET 4.0,则可以使用
dynamic
而不是object
并调用成员,就像它们在那里一样。然后将在运行时检查它,如果名称正确,则执行它。If you are using .NET 4.0 you can use
dynamic
instead ofobject
and invoke the members as if they were there. This will then be checked at runtime, and if the name is correct, execute it.尝试从添加参考文献中查看这一点。它使您可以轻松访问 Excel。
微软Office核心
Microsoft.Office.Interop.Excel
...
可以像这样访问单元格和工作表等:
Try having a look at this from the add references. It gives you useful access to Excel.
Microsoft.Office.Core
Microsoft.Office.Interop.Excel
...
Cells and worksheets etc can be accessed like this: