C#控制台程序套壳打开EXCEL并强制启用宏

发布于:2023-01-09 ⋅ 阅读:(594) ⋅ 点赞:(0)

本文介绍的是程序内嵌套壳,原理是释放到临时文件夹中,并打开该文件
参考资料:https://blog.csdn.net/lzl_li/article/details/117026469
参考资料:https://www.it1352.com/1163446.html

 

   static void Main(string[] args)
        {
            string wj_name = "aaa.xlsm";

            string[] str;
            if (args != null && args.Length > 0)
                str = args;
            else
                str = new string[] { "没有获得参数,程序中定义的字符串。" };
            
            String projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
            System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName +"."+ wj_name);
            byte[] fileBytes = new byte[stream.Length];
            stream.Read(fileBytes, 0, (int)stream.Length);
            

            //进程外部启动新进程(将资源文件存储到磁盘并启动新进程),不受编译平台限制,所有类型APP文件都可以
            string fileName = System.IO.Path.GetTempPath();
            fileName = fileName + "\\"+wj_name;
            System.IO.BinaryWriter bw = new System.IO.BinaryWriter(new System.IO.FileStream(fileName, System.IO.FileMode.Create));
            bw.Write(fileBytes);
            bw.Flush();
            bw.Dispose();

            //方法1:直接打开,则会用默认的打开方式打开该文件
            //System.Diagnostics.Process myPro = System.Diagnostics.Process.Start(fileName, str[0]);            
            //可以在资源文件数据进行加密操作,先将文件加密再加入资源当中,读取时再进行解密,然后执行后续操作。

            //方法2:先打开软件默认启动项的exe,且下面打开方法可强制启用宏
            Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();
            excelApp.Visible = true;
            string workbookPath = fileName;
            Microsoft.Office.Interop.Excel.Workbook excelWorkbook =
           excelApp.Workbooks.Open(workbookPath, 0, false, 5, 0, 0, false,
           Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, 0,
           true, false, 0, true, false, false);
        }


网站公告

今日签到

点亮在社区的每一天
去签到