添加按钮为s preadsheets在.NET(VSTO) [英] Adding buttons to spreadsheets in .NET (VSTO)
问题描述
使用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沿着这些路线(感谢控件添加到文档附加,人们对<一href="http://social.msdn.microsoft.com/Forums/en/vsto/thread/9fcadefb-49e2-4f05-8b73-8aabce8dc40d">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!";
检查<一href="http://blogs.msdn.com/b/eric_carter/archive/2008/07/24/using-vsto-document-features-in-application-level-add-ins.aspx">this后由埃里克·卡特,获取更多信息。
Check this post by Eric Carter for more info.
这篇关于添加按钮为s preadsheets在.NET(VSTO)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!