//声明Excel对象 using Excel = Microsoft.Office.Interop.Excel; //获取当前活动工作簿 app = (Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application"); Excel.Range rg; //声明单元格的对象 rg = app.Range["A1:A10"]; //获取单元格 rg.Select();//选择单元格 string str = app.Range["A1"].Value;//获取A1单元格的值 rg.Interior.color=Color.Blue;//单元格设置颜色 This.Text=(string)app.ActiveCell.Value; //窗口标题等于活动单元格的值 //-------------------------------------------------------------------------------- //遍历工作簿 foreach (Excel.Workbook wb in app.Workbooks) { } //通过数组引用 Excel.Workbook wbk = app.Workbooks[1]; MessageBox.Show(wbk.Name); //弹出消息框显示第一个工作簿的名字 //-------------------------------------------------------------------------------- //处理Excel事件过程 Excel.Worksheet wst = wbk.Worksheets["ak"]; //声明工作表对象 wst.Range["A1:A10"].Value = 56; //赋值单元格 //加事件 wst.SelectionChange += new Excel.DocEvents_SelectionChangeEventHandler(myEvent); Private void myEvent(Excel.Range Target) { Target.Value=56; Target.Merge();//合并单元格 } //取消事件 wst.SelectionChange -= new Excel.DocEvents_SelectionChangeEventHandler(myEvent); public Excel.Application ExcelApp;//声明公共对象变量 //自动往单元格写入字符串 //set ExcelApp = Application ExcelApp = Globals.ThisAddIn.Application; //获得控制权 ExcelApp.ActiveCell.Value = "姚琛来了";