1

我有一个网站,用户使用宏生成 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[] { });
        }
    }
4

1 回答 1

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/

于 2012-06-24T20:09:58.850 回答