动态加载所有COM类型的Excel? [英] Loading all COM types of Excel dynamically?
本文介绍了动态加载所有COM类型的Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的C#应用程序中使用Excel,而不包括dll和东西。我首先要检查是否安装了所需的Excel版本并运行它。
首先我想要获取所需的所有类型,但是我无法掌握:
// works
Type typeExcel = Type.GetTypeFromProgID(Excel.Application);
object excel = Activator.CreateInstance(typeExcel);
对象工作簿= typeExcel.InvokeMember(工作簿,BindingFlags.GetProperty,null,excel,null);
//这不起作用,返回null
键入typeWorkbooks = Type.GetTypeFromProgID(Excel.Workbooks);
没有正确的类型,我无法调用成员。那我做错了什么?我如何加载我需要的所有类型,并知道他们在那里?我现在的Excel版本是2003.
原因如下:如果我包含目标系统上未安装的COM库,我的应用程序将不会启动。如果我动态加载它,可以检查它们的存在,并通知用户缺少的功能。
解决方案
使用动态
。
键入typeExcel = Type.GetTypeFromProgID(Excel.Application);
dynamic excel = Activator.CreateInstance(typeExcel);
excel.Visible = true;
动态工作簿= excel.Workbooks;
workbooks.Add();
workbooks.Add();
另请参阅这个答案。
I want to explore this path of working with Excel dynamically.
I want to use Excel in my C# application without including dlls and stuff. I would just check first if the required Excel version is installed and run it.
First I want to get all the types I need but I can't get hold of them:
// works
Type typeExcel = Type.GetTypeFromProgID("Excel.Application");
object excel = Activator.CreateInstance(typeExcel);
object workbooks = typeExcel.InvokeMember("Workbooks", BindingFlags.GetProperty, null, excel, null);
// this doesn't work, returns null
Type typeWorkbooks = Type.GetTypeFromProgID("Excel.Workbooks");
Without the correct types I can't invoke members. So what am I doing wrong ? How do I load all types I need and know that they are there ? My current Excel version is 2003.
Reasons for this: If I include COM Libraries that are not installed on the target system my application wont start. If I load them dynamically I can check for their existence and notify the user about missing functionality.
解决方案
Use dynamic
.
Type typeExcel = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(typeExcel);
excel.Visible = true;
dynamic workbooks = excel.Workbooks;
workbooks.Add();
workbooks.Add();
Also see this answer.
这篇关于动态加载所有COM类型的Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文