VSTO for excel入门基础知识
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
常用语法
IF 语句
private void button11_Click(object sender, EventArgs e) {
int i;
i = -25;//-2147483648 到 2147483647 ,有符号 32 位整数
if (i > 0)
{
MessageBox.Show(i + "是正数");
}
else if (i == 0)
{
MessageBox.Show(i + "是零");
}
else
{
MessageBox.Show(i + "是负数");
}
//更多分之使用Switch结构,类似select case
}
While语句
private void button12_Click(object sender, EventArgs e) {
int i;
i = 0;
while (i < 10)//括号内条件成立,不断执行花括号内容
{
i = i + 1;
}
this.button14.Text = i.ToString();//
}
FOR语句
private void button13_Click(object sender, EventArgs e) {
int i;
//;
int sum = 0;
for (i = 1; i <= 10; i++)//i++自加的意思
if (i <= 5)//if (i <=5) {break;}
{
sum = sum + i;
}
else
{
break;//跳出FORB不再执行。
}
}
this.button13.Text = sum.ToString();
}
FOR语句
private void button14_Click(object sender, EventArgs e)
{
//int i;这句声明变量,作用于整个单机过程
int sum = 0;
for (int i = 1; i <= 10; i++)//也可在FOR循环内部声明直接加变量int,只做用内部
{
if (i==6)//i等于6
{continue;}//当I=6跳过不加,到7的时候继续加,比55小于6
//严格区分break 和continue语句
{
sum = sum + i;
}
}
this.button13.Text = sum.ToString();
}
FOR Each 语句
private void button15_Click(object sender, EventArgs e)
{
int[] arr = { 2, 5, 7 };//声明数组。
arr[0]=2 arr[1]=5 arr[2]=7
//方法1
//foreach (int b in arr)
//{
// MessageBox.Show(b.ToString());
//}
//方法2
for (int i = 0; i <= 2; i++)
MessageBox.Show(arr[i].ToString());
}
}
文件对话框
private void button1_Click(object sender, EventArgs e) {
this.openFileDialog1.ShowDialog();
this.textBox1.Text = this.openFileDialog1.FileName; }
添加按钮
private void button2_Click(object sender, EventArgs e) {
TextBox tx=new TextBox();
tx.Text = "自动添加文本框";
tx.BackColor = Color.Cyan;
tx.Left = 50;
tx.Top = 150;
this.Controls.Add(tx);
}
关闭窗体
private void button1_Click(object sender, EventArgs e) {
this.Dispose();//关闭自己
}
定义变量
private void button10_Click(object sender, EventArgs e) {
string s = "张三";
button14.Text = s;
}
窗体显示必须关闭
private void button8_Click(object sender, EventArgs e) {
Form2 f2 = new Form2();//类模块创建对象
f2.ShowDialog();//Dialog必须关闭窗体2,才能编辑窗体1 }
显示或者隐藏控件
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
//if (this.checkBox1.Checked == true)
// {this.button8.Visible = true;
// }
//else
//{
// this.button8.Visible = false;
//}
this.button8.Visible = this.checkBox1.Checked;//等同于以上几句话
}
触发dataGridView的CellClick事件
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
string no = dataGridView1.Rows[e.RowIndex].Cells[0].Value.T oString();
string name=dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
TextBox1.Text=name;
TextBox2.Text=no;
}
操作EXCEL对象
获取当前运行的EXCEL
using excel = Microsoft.Office.Interop.Excel;//用EXCEL代替了后面的一长串
excel._Application app;//定义app
private void button1_Click(object sender, EventArgs e)
{
//获取当前运行的EXCEL。
本句可以优先放到窗体启动事件,也可以只放在点击事件//app =
(excel._Application)System.Runtime.InteropServices.Marshal.GetActiveObject("excel.Appli cation");//设置app等于当前运行的EXCEL。
固定语句
MessageBox.Show(erName);//获取当前EXCEL的用户名
excel.Range rg;//声明range对象
rg = app.Range["a1:d10"];//定义rg的范围
//rg.Select();//选择rg的范围
rg.Interior.Color = Color.Blue;//更改rg的背景色
double d = app.ActiveCell.Value;
MessageBox.Show(d.ToString());
}
操作workbook对象
private void button2_Click(object sender, EventArgs e)
{
foreach (excel.Workbook wbk in app.Workbooks)//遍历所有打开工作簿,工作表方法类似
{
MessageBox.Show("当前已打开的工作簿有:"+);//显示当前运行工作簿的名字
}
}
获取第N个打开的
private void button3_Click(object sender, EventArgs e)//下标数字引用
{
excel.Workbook wbk = app.Workbooks[1];//获取第二个打开的工作簿名称
MessageBox.Show("当前已打开的工作簿有:" + );//显示当前运行工作簿的名字
}
EXCEL事件
开启EXCEL事件
private void button4_Click(object sender, EventArgs e)//点击按钮产生自定义事件
{
MessageBox.Show("sheet1产生选择合并单元格事件");
excel.Workbook wbk = app.Workbooks[1];//获取第1个打开的工作簿名称
excel.Worksheet wst = wbk.Worksheets["Sheet1"];//再获取名字为A的工作表//wst.Range["c1:e10"].Value = 99;
wst.SelectionChange += new excel.DocEvents_SelectionChangeEventHandler(选择事件);//定义工作表选择事件,名字自定义
}
private void选择事件(excel.Range Target)//如果类型不匹配,右键上面定义事件-转到定义,复制后面变量类型,补上EXCEL.
{
Target.Merge();//合并单元格,可以选择任何事件
}
关闭EXCEL事件
private void button5_Click(object sender, EventArgs e)//取消事件。
复制上面内容,+=更改成-=
{
MessageBox.Show("停止选择事件");
excel.Workbook wbk = app.Workbooks[1];//获取第1个打开的工作簿名称//或者直接抬高定义范围,放到form 下面
excel.Worksheet wst = wbk.Worksheets["Sheet1"];//再获取名字为A的工作表//或者
直接抬高定义范围,放到form 下面
wst.SelectionChange -= new excel.DocEvents_SelectionChangeEventHandler(选择事件);//定义工作表选择事件,名字自定义//+=更改成-=
}
Robbin加载项
注意事项
FORM1设置
using Excel = Microsoft.Office.Interop.Excel;//将THISADDIN 最后三句复制到FORM1
using Office = Microsoft.Office.Core;//将THISADDIN 最后三句复制到FORM1
using Microsoft.Office.Tools.Excel;//将THISADDIN 最后三句复制到FORM1
namespace myfirstAddin
{
public partial class Form1 : Form
{
excel._Application app;//定义app
public Excel.Application excelapp;
public Form1()
{
InitializeComponent();//窗体启动事件//类似于load事件
excelapp = Globals.ThisAddIn.Application;//excelapp 取得了对EXCEL的控制权//继续复制该句
}
private void Form1_Load(object sender, EventArgs e)//窗体启动事件
{
//获取当前运行的EXCEL。
本句可以优先放到窗体启动事件,也可以只放在点击事件 app =
(excel._Application)System.Runtime.InteropServices.Marshal.GetActiveObject("excel.Appli cation");//设置app等于当前运行的EXCEL。
固定语句
}
ROBBIN 设置
public partial class Ribbon1
{
public Excel._Application excelapp;
private void Ribbon1_Load(object sender, RibbonUIEventArgs e)//功能区启动,获得控制权
{
excelapp = Globals.ThisAddIn.Application;//excelapp 取得了对EXCEL的控制权//继续复制该句
}
THIS ADDIN设置
public partial class ThisAddIn
{
public Excel.Application excelapp;//公有声明一个EXCEL类型
单元格值写入文本框
private void button1_Click(object sender, EventArgs e)
{
excelapp.ActiveCell.Value = this.textBox1.Text; //this相当于form1 }
文本框值写入单元格
private void button2_Click(object sender, EventArgs e)
{
double d = excelapp.ActiveCell.Value;
//this.textBox2.Text = excelapp.ActiveCell.get_Address();//Address; Excel.Workbook wbk = excelapp.Workbooks.Add();
Excel.Range rg;//vba:dim rng as range
rg = excelapp.Range["a1:d10"];
MessageBox.Show(rg.Address);
this.textBox2.Text = d.ToString();
int i = excelapp.Workbooks.Count;
MessageBox.Show(i.ToString());
//excelapp.Workbooks.Add();//新建工作簿等类似语法
}。