dynamic加载程序集

我需要dynamic加载一个程序集。 目前我正在使用“Microsoft.Office.Microsoft.Office.Interop.dll”。 我需要打开一个excel文件,并从中获取表单和数据。 谁能告诉我怎么办?

Assembly SampleAssembly = Assembly .Load("Microsoft.Office.Interop.Excel, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"); if (SampleAssembly != null) { Type type = SampleAssembly.GetType("Microsoft.Office.Interop.Excel.ApplicationClass"); Object AppClass = Activator.CreateInstance(type); //How will I get ApplicationClass object and Workbooks objects here? } 

充分利用dynamic

  Type type = SampleAssembly.GetTypes().Single(t => t.Name == "ApplicationClass"); dynamic appClass = Activator.CreateInstance(type); var workbook = appClass.Workbooks.Open("C:\\test.csv"); var worksheet = workbook.Worksheets[1]; 

如果你不能/不想使用dynamic,我会定义一个项目,其中有一个接口(或一组接口),用于暴露你需要使用的excel方法/对象,你可以在代码中引用实际的excel dll。 然后在不同的项目中引用excel并将其dynamic加载。

像这样的东西:

 public interface IExcelApp { IExcelWorkbook OpenWbk(string aPath); // other stuff } public interface IExcelWorkbook { //the stuff you need } 

实现(在一个我称之为ExcelBridge的项目中):

 public class ExcelApp : IExcelApp { private ApplicationClass _app; public ExcelApp() { } public IExcelWorkbook OpenWbk(string aPath) { return new ExcelWorkbook(_app.Workbooks.Open(aPath)); } } public class ExcelWorkbook : IExcelWorkbook { private Workbook _wbk; public ExcelWorkbook(Workbook aWbk) { _wbk = aWbk; } } 

那么你的代码可能是这样的:

 Assembly SampleAssembly = Assembly.Load("ExcelBridge", Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"); if (SampleAssembly != null) { Assembly YourExcelAssembly = Assembly.Load("ExcelBridge.dll"); Type type = YourExcelAssembly .GetType("ExcelApp"); IExcel AppClass = Activator.CreateInstance(type) as IExcelApp; //now you can write: IExcelWorkBook aWbk = AppClass.Open("your xls path"); } 

这里是你如何在C#

 using Microsoft.Office.Interop.Excel; Type type = SampleAssembly.GetType("Microsoft.Office.Interop.Excel.ApplicationClass"); var application = Activator.CreateInstance(type); var workbook = application.Workbooks.Open(fileName); var worksheet = workbook.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet; 

如果您使用.NET 3.5,则不能使用dynamic关键字。 您唯一的select是使用@Francesco所描述的一系列代理类(手动编写或自动生成)。 但是,如果您需要访问大量的Excel类/函数/属性,手动方法真的很麻烦。 你必须为你需要的每个成员编写一个函数/属性代理。

pipe理这一点的关键在于,不是编写大量的代理函数,而是将所有与Excel相关的代码放在一个单独的软件层(如数据访问层,DAL)中,放置在不同的库项目中。 这个层只提供一些高级接口(只有less数成员函数)给应用程序,这样应用程序甚至不知道MS Excel自动化的用法。

请注意,即使您不想dynamic加载程序集,对Excel访问代码的封装也是一个很好的做法。 例如,如果发现令人沮丧,您可以使用另一种技术(如Microsoft.ACE.OLEDB.12.0提供程序,用于读取/写入Excel文件的 ADO.NET)轻松replaceOffice自动化,这在x64平台中也令人沮丧!

例如,如果要从Excel文件抓取工作表和数据,可以在名为Utils.OfficeAutomation (不参考Microsoft.Office.Interop.Excel.dll )的项目中提供以下界面:

 interface IExcelLayer { ... bool ImportFrom(string fileName, string sheetName, out object[,] cells, bool skipHeaderRow = true); // a few other members... ... } 

然后在一个名为Utils.OfficeAutomation.Impl的单独项目中实现它,它引用Utils.OfficeAutomation和真正的Microsoft.Office.Interop.Excel.dll

 class ExcelMagician : IExcelLayer { ... bool ImportFrom(string fileName, string sheetName, out object[,] cells, bool skipHeaderRow = true) { Excel.Application excelApp = new Excel.Application() ... } ... } 

主应用程序只有对Utils.OfficeAutomation的引用。 作为回报, Utils.OfficeAutomation以某种方式负责dynamic查找和加载Utils.OfficeAutomation.Impl ,或者在出现问题时产生错误:

 IExcelLayer m_instance = null; ... try { Assembly assembly = Assembly.Load("Utils.OfficeAutomation.Impl"); Type excelType = assembly.GetType("Utils.OfficeAutomation.Impl.ExcelMagician"); m_instance = (IExcelLayer) Activator.CreateInstance(excelType); } catch (Exception e) { throw new Exception("Couldn't load Excel assembly.") } 

如果未安装适当版本的Microsoft Excel,则加载程序集Utils.OfficeAutomation.Impl失败,或调用成员(如ImportFrom生成exception。

您最后需要在Utils.OfficeAutomation内部编写包装类作为Excel访问层或Excelfunction的网关。