动态加载所有COM类型的Excel? [英] Loading all COM types of Excel dynamically?
本文介绍了动态加载所有COM类型的Excel?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想探索动态使用Excel的这条路径。
我想在我的C#应用程序中使用Excel,而不包括dll和东西。我只是先检查是否安装了所需的Excel版本并运行它。
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");
没有正确的类型我不能调用成员。那么我做错了什么?如何加载我需要的所有类型,并知道他们在那里?我当前的Excel版本是2003.
原因:如果我包括没有安装在目标系统上的COM库,我的应用程序不会启动。如果我动态加载它们,我可以检查他们的存在,并通知用户缺少的功能。
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.
推荐答案
使用 dynamic
。
Type typeExcel = Type.GetTypeFromProgID("Excel.Application");
dynamic excel = Activator.CreateInstance(typeExcel);
excel.Visible = true;
dynamic workbooks = excel.Workbooks;
workbooks.Add();
workbooks.Add();
另请参阅此答案。
这篇关于动态加载所有COM类型的Excel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文