返回Excel催化剂目录

产品文档 / Excel催化剂

第2波-自动检测Excel的位数选择对应位数的xll文件安装

为了还原一个干净无侵扰的网络世界,本文将不进行大规模地分发,若您觉得此文有用,不妨小范围地分享到真正有需要的人手中 Excel插件的部署问题难倒了不了的用户,特别是VSTO的部署,用ExcelDna开发的xll文件部署方便,不挑用户机器环境,是其开发Excel插件的一大优势。 其开发出来的xll文件

为了还原一个干净无侵扰的网络世界,本文将不进行大规模地分发,若您觉得此文有用,不妨小范围地分享到真正有需要的人手中

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\n请注意关闭Excel程序并保存后内容后再运行,防止数据丢失造成损失!");
                Console.WriteLine("请退出Excel后按Y键继续,,按其他键退出程序。\r\n若未发现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, @"Microsoft\AddIns");
            if (Directory.Exists(dstDir))
            {
                Directory.CreateDirectory(dstDir);
            }

            return dstDir;
        }
    }
}

开源地址为:https://github.com/minren118/ExcelUdfByExcelCuiHuaJi,不妨对您有帮助时帮忙在GtiHub上点个赞。

内容插图