Excel插件的部署问题难倒了不了的用户,特别是VSTO的部署,用ExcelDna开发的xll文件部署方便,不挑用户机器环境,是其开发Excel插件的一大优势。
其开发出来的xll文件,最终还是需要考虑用户机器Excel位数的问题,32位的Excel安装32位的xll文件,同理64位安装64位的xll。
如何判断用户机器是32位还是64位的OFFICE,并将对应位数的xll文件安放到用户机器上,这个问题之前笔者曾经花时间找过资料,最终实现了想要的功能效果。
同样的因笔者非专业程序猿,可能写出来的代码严谨性有限,仅供参考。
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.IO; namespace _20171207自定义函数安装 { class Program { private static string dstFileName = "ExcelUdf.xll";//用户电脑上的文件名 [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] [return: MarshalAs(UnmanagedType.Bool)] internal static extern bool IsWow64Process([In] IntPtr process, [Out] out bool wow64Process); static void Main(string[] args) { InstallExcelUdfAddins(); } private static void InstallExcelUdfAddins() { Console.WriteLine("正在安装中,请稍等。"); dynamic excelComObj; bool isExitExcel = false; try { excelComObj = Marshal.GetActiveObject("Excel.Application"); Console.WriteLine("检测到当前有已打开的Excel程序r 请注意关闭Excel程序并保存后内容后再运行,防止数据丢失造成损失!"); Console.WriteLine("请退出Excel后按Y键继续,,按其他键退出程序。r 若未发现Excel程序已打开,同样按Y键程序强制继续,按其他键退出程序。"); string input = Console.ReadLine(); if (input.ToUpper() == "Y") { isExitExcel = true; } else { return; } } catch (Exception) { excelComObj = CreateExcelApp(); isExitExcel = true; } if (isExitExcel == true) { bool is64BitWindows = Environment.Is64BitOperatingSystem; bool retVal; bool is64BitProcess = false; foreach (var item in Process.GetProcesses()) { if (item.ProcessName.ToLower() == "excel") { try { if (is64BitWindows)//仅当系统是64位时才需要判断,若为32位肯定是32位的OFFICE { is64BitProcess = !(IsWow64Process(item.Handle, out retVal) && retVal); } } catch (Exception) { } item.Kill();//强制性退出,已经通知过用户关闭Excel再运行,未关闭完的就强制退出。 } } CopyFileToAddinsDirectoryAndStartAddin(is64BitProcess); Console.WriteLine("安装完成,请按任意键退出程序!"); Console.ReadKey(); } } private static dynamic CreateExcelApp() { dynamic excelComObj; System.Type oType = System.Type.GetTypeFromProgID("Excel.Application"); excelComObj = System.Activator.CreateInstance(oType); excelComObj.DisplayAlerts = false; return excelComObj; } private static void CopyFileToAddinsDirectoryAndStartAddin(bool is64BitProcess) { string dstDir = GetdstDir(); string srcfilePath = Path.Combine(Path.GetTempPath(),dstFileName ); byte[] resFilebytes; if (is64BitProcess) { resFilebytes = Properties.Resources.ExcelUDF_AddIn64_packed; } else { resFilebytes = Properties.Resources.ExcelUDF_AddIn_packed; } using (FileStream fileStream = new FileStream(srcfilePath, FileMode.Create)) { fileStream.Write(resFilebytes, 0, resFilebytes.Length); } File.Copy(srcfilePath, Path.Combine(dstDir, dstFileName), true); dynamic excelApp = CreateExcelApp(); foreach (var item in excelApp.AddIns) { string addinsName = item.Name; if (addinsName==dstFileName) { item.Installed = true; } } excelApp.Quit(); Marshal.ReleaseComObject(excelApp); excelApp = null; GC.Collect(); } private static string GetdstDir() { string appDataDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string dstDir = Path.Combine(appDataDir, @"MicrosoftAddIns"); if (Directory.Exists(dstDir)) { Directory.CreateDirectory(dstDir); } return dstDir; } } }
开源地址为:https://github.com/minren118/ExcelUdfByExcelCuiHuaJi,不妨对您有帮助时帮忙在GtiHub上点个赞。
登录Github后点击红框给个星星
页面更新:2024-05-20
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号