踩坑,找了半天的资料没解决,报错如下,明明与EXCEL VBA一样的代码。。。
最后自己分析VBA与C# 操作的区别,VBA是打开Excel界面操作,C# 是未开启Excel出现界面操作的
private static void Main(string[] args)
{
dynamic xlsApp = Microsoft.VisualBasic.Interaction.CreateObject("Excel.Application");
xlsApp.Visible = true;//关键的代码,否则无法完成安装
xlsApp.Workbooks.Add();
xlsApp.Windows(1).Visible = true;
xlsApp.ScreenUpdating = false;
try
{
var xllfi = new FileInfo(@"C:\MyExcelDna.xll");
if (!xllfi.Exists) return;
bool flag = false;
for (int i = xlsApp.Addins.Count; i >= 1; i--)
{
var addin = xlsApp.Addins[i];
if (addin.Name == xllfi.Name)
{
if (addin.FullName != xllfi.FullName)
{
addin.Installed = false;
File.Delete(addin.FullName);
Console.WriteLine("removed old xll complete...");
}
else
{
if (addin.Installed = false)
{
addin.Installed = true;
flag = true;
Console.WriteLine("keep xll complete...");
}
}
}
}
if (!flag)
{
xlsApp.DefaultFilePath = xllfi.DirectoryName;
var addin = xlsApp.AddIns.Add(xllfi.FullName, false);//大部分此步骤报错的
addin.Installed = false;
addin.Installed = true;
Console.WriteLine("install xll complete...");
}
xlsApp.Quit();
if (xlsApp != null)
{
Marshal.ReleaseComObject(xlsApp);
}
}
catch (Exception ex)
{
if (xlsApp != null)
{
Console.WriteLine(ex.StackTrace);
Marshal.ReleaseComObject(xlsApp);
}
}
}
}
标签:插件,false,addin,C#,EXCEL,Installed,xlsApp,true,xllfi From: https://www.cnblogs.com/NanShengBlogs/p/18665836