1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Xml.Linq; 6 using Excel = Microsoft.Office.Interop.Excel; 7 namespace ExcelAddInDemo 8 { 9 using Microsoft.Office.Tools; ThisAddIn 11 { 12 public CustomTaskPane _MyCustomTaskPane = null; ThisAddIn_Startup(object sender, System.EventArgs e) 15 { 16 UCTaskPane taskPane = new UCTaskPane(); ); _MyCustomTaskPane.Visible = true; 20 _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop; 21 22 UCPaneLeft panLeft = new UCPaneLeft(); ); 24 _MyCustomTaskPane.Width = 200; 25 _MyCustomTaskPane.Visible = true; 26 _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionLeft; 27 28 UCTaskGrid panRight = new UCTaskGrid(); ); 30 _MyCustomTaskPane.Width = 200; 31 _MyCustomTaskPane.Visible = true; 32 _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight; 33 34 UCLog panLog = new UCLog(); ); 36 _MyCustomTaskPane.Width = 60; 37 _MyCustomTaskPane.Visible = true; 38 _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionBottom; Hook into the workbook open event .Application.WorkbookActivate += Application_WorkbookActivate; 43 //test } Application_SheetSelectionChange(object Sh, Excel.Range Target) 48 { 49 if (this.Application != null) 50 { 51 this.Application.Caption = this.Application.ActiveCell.Address.ToString();//$A$1 $A$1 } 56 } Application_WorkbookActivate(Excel.Workbook Wb) 59 { 60 //using Microsoft.Office.Tools.Excel 和 using Microsoft.Office.Interop.Excel 都有worksheet等,容易混淆 Excel._Worksheet ws = (Excel._Worksheet)this.Application.ActiveWorkbook.ActiveSheet; ; r=2,c=2; ((Excel.Range)ws.Cells[r, c]).Value2 = ; 68 ((Excel.Range)ws.Cells[r, c]).Interior.Color =System.Drawing. ColorTranslator.ToOle(System.Drawing.Color.Red); ((Excel.Range)ws.Cells[r, c]).Style.Font.Bold = true; format , )).Font.Bold = true; , )).Font.Italic = true; , )).Font.Color = System.Drawing.Color.FromArgb(96, 32, 0).ToArgb(); , )).Font.Name = ; , )).Font.Size = 15; Excel.Range range = ((Microsoft.Office.Interop.Excel.Range)ws.get_Range(, )); 81 Excel. Borders border = range.Borders; 82 border[Excel.XlBordersIndex.xlEdgeBottom].LineStyle =Excel. XlLineStyle.xlContinuous; 83 border.Weight = 2d; 84 border[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous; 85 border[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous; 86 border[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous; ws.Cells[; ; ; 91 } ThisAddIn_Shutdown(object sender, System.EventArgs e) 93 { 94 } VSTO 生成的代码 设计器支持所需的方法 - 不要 100 /// 使用代码编辑器修改此方法的内容。 InternalStartup() 103 { 104 this.Startup += new System.EventHandler(ThisAddIn_Startup); 105 this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); 106 } } 110 }