从网站上远程机器上的程序执行Excel宏

时间:2012-06-22 02:07:07

标签: c# .net excel ole-automation

我有一个网站,用户使用宏生成Excel报告,当我尝试在本地计算机上运行它时,它会完美生成并在Excel中运行宏。当我将其发布到服务器并同时登录(RDP打开会话)并尝试从该服务器外部的浏览器运行它时,它也按预期运行。当我在服务器(RDP)中注销然后在服务器外部的浏览器(即从我的机器)中运行它时,问题就出现了宏不运行但创建我的Excel。

这是我正在使用的代码

public class Report
    {
        protected Workbook Workbook { get; set; }
        protected Application Excel { get; set; }

        public void RunReport()
        {
            // Launch Excel on the server
            Excel = new Application
            {
                DisplayAlerts = false,
                ScreenUpdating = false,
                Visible = false
            };

            // Load the workbook template  
            Workbook = Excel.Workbooks.Open(@"D:\Book1.xlt");

            // Execute macros that generates the report, if any
            ExecuteMacros();

            Workbook.SaveAs(@"D:\Ray'sTesting.xls", XlFileFormat.xlExcel8);
            QuitExcel();

        }
        private void QuitExcel()
        {
            if (Workbook != null)
            {
                Workbook.Close(false);
                Marshal.ReleaseComObject(Workbook);
            }

            if (Excel != null)
            {
                Excel.Quit();
                Marshal.ReleaseComObject(Excel);
            }
        }        
        private void ExecuteMacros()
        {


            const string legacyModuleName = "Module1";
            const string legacyMacroName = "myMacro";

            bool legacyMacroExists = false;
            try
            {
                var legacyMacroModule = Workbook.VBProject.VBComponents.Item(legacyModuleName);
                if (legacyMacroModule != null)
                {
                    int legacyMacroStartLine = legacyMacroModule.CodeModule.ProcStartLine[legacyMacroName, Microsoft.Vbe.Interop.vbext_ProcKind.vbext_pk_Proc];
                    legacyMacroExists = legacyMacroStartLine > 0;
                }
            }
            catch (Exception)
            {
                legacyMacroExists = false;
            }

            if (!legacyMacroExists)
            {
                return;
            }

            // VBA code for the dynamic macro that calls the CI2 legacy macro
            var moduleCode = new StringBuilder();
            moduleCode.AppendLine("Public Sub LaunchLegacyMacro()");
            moduleCode.AppendLine(string.Format("{0}.{1}", legacyModuleName, legacyMacroName));
            moduleCode.AppendLine("End Sub");

            // Add the dynamic macro to the ThisWorkbook module
            var workbookMainModule = Workbook.VBProject.VBComponents.Item("ThisWorkbook");
            workbookMainModule.CodeModule.AddFromString(moduleCode.ToString());

            // Execute the dynamic macro
            Microsoft.VisualBasic.Interaction.CallByName(Workbook, "LaunchLegacyMacro", Microsoft.VisualBasic.CallType.Method, new object[] { });
        }
    }

1 个答案:

答案 0 :(得分:1)

每当我们通过

运行excel宏时,我都会通过编辑注册表来实现这一点
private static void ModifyExcelSecuritySettings()
{
    // Make sure we have programmatic access to the project to run macros
    using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\14.0\Excel\Security", true))
    {
        if (key != null)
        {
            if ((int)key.GetValue("AccessVBOM", 0) != 1)
            {
                key.SetValue("AccessVBOM", 1);
            }
            key.Close();
        }
    }
}

所以代码看起来应该是这样的

public void RunReport()
{
    ModifyExcelSecuritySettings();

    // Launch Excel on the server
    Excel = new Application
    {
        DisplayAlerts = false,
        ScreenUpdating = false,
        Visible = false
    };

.....

我还为完整的解决方案创建了一篇博文,您可以在这里查看

http://anyrest.wordpress.com/2012/06/22/programmatic-execution-excel-macro-on-remote-machine-from-a-website/