如何加载一个Excel加载项使用互操作 [英] How to load an Excel Addin using Interop

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

问题描述

我有一个外接程序,我想通过从C#WinForms应用程序Excel的互操作来调用。



我不能得到的插件等加载,除非我卸载每次resinstall它(apparantly是与 Excel中,当您使用互操作不加载加载项 - 顺便说一句,不能​​让他们的例子在C#中工作)。很不幸,这是缓慢的,恼人的用户,所以我需要简化它。



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



我已经搜查,搜查,但一切我找到谷歌给出安装/重新安装的解决方案。有没有其他办法?该插件在安装,我只是希望Excel加载它



这就是我现在在做(从google'd建议采取):

  //遍历所有的加载项,如果你发现它卸载。 
的foreach(外接程序加载项在excel.AddIns)
如果(addIn.Name.Contains(我的外接程序))
addin.Installed = FALSE;

//安装插件
VAR插件= excel.AddIns.Add(my_addin.xll,FALSE);
addin.Installed = TRUE;


解决方案

过了一会儿,我发现了隐藏在陌生的地方,答案在 MS帮助:和的这个博客帖子



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

  VAR练成=新的应用程序(); 
变种工作簿= 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!

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

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