Adding buttons to spreadsheets in .NET (VSTO)

前端 未结 2 880
忘掉有多难
忘掉有多难 2021-02-15 07:57

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:49

    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");
    }
    

提交回复
热议问题