如何使用Interop加载Excel Addin [英] How to load an Excel Addin using Interop

查看:134
本文介绍了如何使用Interop加载Excel Addin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AddIn,我想从C#winforms应用程序通过Excel互操作调用。



我无法获取加载等加载,除非我卸载并每次树脂安装(这与 Excel不加载插件时使用互操作 - btw,不能让他们的例子在C#中工作)。不幸的是,这对用户来说很慢,很烦人,所以我需要简化它。



我想拥有一个Excel实例,但加载已经安装的插件,而不强制此安装/重新安装问题。



我已搜索并搜索,但是在Google上找到的所有内容都会提供安装/重新安装的解决方案。还有什么办法吗?加载项已安装,我只是想要excel加载它。



这是我目前正在做的(取自google'd的建议):

  //循环加载项,如果您发现它卸载它。 
foreach(AddIn addIn in excel.AddIns)
if(addIn.Name.Contains(My Addin))
addin.Installed = false;

//安装addin
var addin = excel.AddIns.Add(my_addin.xll,false);
addin.Installed = true;


解决方案

过了一会儿,我发现隐藏在奇怪的地方的答案在 MS帮助中:此博文



这不是你需要的所有信息。注意事项:您必须至少有一个工作簿打开或其他Excel barfs。这里有一些粗略的代码开始:

  var excel = new Application(); 
var workbook = excel.workbooks.Add(Type.Missing);
excel.RegisterXLL(pathToXll);
excel.ShowExcel();

如果你想关闭临时工作簿(如果你已经运行了一些宏等)记得用Marshal.ReleaseComObject!的大量调用来整理所有东西!


I have an AddIn which I want to invoke through Excel interop from a C# winforms application.

I can't get the addin etc. to load unless I uninstall and resinstall it each time (this is apparantly something to do with Excel not loading addins when you use interop - btw, can't get their example to work in C#). Unfortunately this is slow and annoying to the user so I need to streamline it.

I want to have one instance of Excel but load an already installed addin without forcing this install/reinstall problem.

I've searched and searched but everything I find on google gives the solution to install/reinstall. Is there any other way? The add-in is installed, I just want excel to load it.

This is what I am doing at the moment (taken from google'd advice):

// loop over the add-ins and if you find it uninstall it.
foreach (AddIn addIn in excel.AddIns)
    if (addIn.Name.Contains("My Addin"))
        addin.Installed = false;

    // install the addin
    var addin = excel.AddIns.Add("my_addin.xll", false);
        addin.Installed = true;

解决方案

After a while I found the answer hidden in strange places in the MS help: and this blog post.

That isn't all the info you need though. Things to note: you must have at least one workbook open or otherwise Excel barfs. Here's some rudementry code to get started:

var excel = new Application();
var workbook = excel.workbooks.Add(Type.Missing);
excel.RegisterXLL(pathToXll);
excel.ShowExcel();

If you want you can close the temporary workbook (if you've run some macros etc.) and remember to tidy everything up with plenty of calls to Marshal.ReleaseComObject!

这篇关于如何使用Interop加载Excel Addin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆