Adding buttons to spreadsheets in .NET (VSTO)

前端 未结 2 551
故里飘歌
故里飘歌 2021-02-15 08:01

Using VSTO or some related technology, is it possible to programmatically embed a button in a cell of an Excel worksheet, and configure it to call a C# function when it is click

2条回答
  •  说谎
    说谎 (楼主)
    2021-02-15 08:33

    Here is the code which works for me in VSTO Add-in (modified version of Mathias's answer):

    using Excel = Microsoft.Office.Interop.Excel;
    using ExcelTools = Microsoft.Office.Tools.Excel;
    
    public void AddButtonToWorksheet()
    {
      Excel.Worksheet worksheet = (Excel.Worksheet)Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
      ExcelTools.Worksheet vstoSheet = Globals.Factory.GetVstoObject(worksheet);
    
      Button button = new Button();
      button.Text = "Dynamic Button!";
      vstoSheet.Controls.AddControl(
        button, 50, 50, 100, 50, "TestButton");
    }
    

提交回复
热议问题