添加按钮为s preadsheets在.NET(VSTO)按钮、preadsheets、VSTO、NET

2023-09-03 16:12:35 作者:所有的深爱都是秘密

使用VSTO或一些相关的技术,是否有可能以编程方式嵌入到Excel工作表的单元格按钮,并将其配置为调用C#功能被点击时?

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 clicked?

如何?

感谢。

推荐答案

随着VSTO自定义文件(即一个工作簿与.net code附后),您可以添加并在运行时删除控件的工作表项目。下面code描述了一个思路:

With a VSTO document customization (i.e., a Workbook with .Net code attached), you can add and remove controls at runtime to the Worksheets of the project. The following code illustrates the idea:

 public partial class Sheet1
 {
     private void Sheet1_Startup(object sender, System.EventArgs e)
     {
        var button = this.Controls.AddButton(10, 10, 50, 50, "My Button");
        button.Text = "My Button";
        button.Click += new EventHandler(button_Click);
     }

     void button_Click(object sender, EventArgs e)
     {
        MessageBox.Show("I was clicked!");
     }

您也可以动态地通过使用VSTO code沿着这些路线(感谢控件添加到文档附加,人们对VSTO论坛那一个):

You could also add controls dynamically to documents via a VSTO add-in, using code along these lines (thanks to people on the VSTO forum for that one):

var workSheet = (Excel.Worksheet) sheet;
var vstoSheet = workSheet.GetVstoObject();
var button = vstoSheet.Controls.AddButton(50, 50, 100, 50, "Test");
button.Text = "Dynamic Button!";

检查this后由埃里克·卡特,获取更多信息。

Check this post by Eric Carter for more info.