简体   繁体   中英

How can we run the excel macro of an existing Excel sheet using C#?

嗨,我正在尝试使用 C#(visual studio)自动打开一个 excel 工作簿并为 Web 应用程序运行其宏,有人可以建议这是否可行。

Hi I have found a solution on web for how to run Excel macros of aldready existing application, and runs its macro.

Here name of macro is : macro from workbook Macro.xlsm

Below is the code snippet :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop;
using Excel = Microsoft.Office.Interop.Excel;


 namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btn_Click(object sender, EventArgs e)
    {

        //~~> Define your Excel Objects
        Excel.Application xlApp = new Excel.Application();

        Excel.Workbook xlWorkBook;

        //~~> Start Excel and open the workbook.
        xlWorkBook = xlApp.Workbooks.Open("C:\\Users\\d.b.venkatachalam\\Desktop\\Macro.xlsm");
        xlApp.Visible = true;

        //~~> Run the macros by supplying the necessary arguments
        xlApp.Run("Macro");

    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM