EXCEL--VSTO项目搭建
程序集引用 : Microsoft.Office.Interop.Excel
1、选择.net framework4.6 框架新建VSTO项目
2、添加可视化Ribbon
3、设置Ribbon菜单的ControlIdType=Custom(使菜单作为独立的选项卡显示)
4、获取属性 public Excel.Application ExcelApp= Globals.ThisAddIn.Application;
private void button1_Click(object sender, RibbonControlEventArgs e) { ExcelApp = Globals.ThisAddIn.Application; int TempInt = Globals.ThisAddIn.Application.Hwnd; RefreshRightPane(TempInt); //设置面板可见性 RightPane.Visible = true; } //窗体句柄字典 private Dictionary<int, CustomTaskPane> HwndPaneDic = new Dictionary<int, CustomTaskPane> { }; private CustomTaskPane RightPane { get; set; } //侧边面板 /// <summary> /// 重新绑定右侧面板 /// </summary> /// <param name="HwndInt">当前窗体的句柄</param> private void RefreshRightPane(int HwndInt) { if (HwndPaneDic.ContainsKey(HwndInt)) { RightPane = HwndPaneDic[HwndInt]; } else { ////创建控件 System.Windows.Forms.UserControl rightPanel = null; ////添加控件 RightPane = Globals.ThisAddIn.CustomTaskPanes.Add(rightPanel, "这里写窗体名称"); ////设置在右侧显示 RightPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight; ////禁止用户修改位置 RightPane.DockPositionRestrict = Microsoft.Office.Core.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange; ////事件 RightPane.VisibleChanged += new EventHandler(CustomPane_VisibleChanged); //添加到字典 HwndPaneDic.Add(HwndInt, RightPane); } } /// <summary> /// 侧边面板事件,用于保持按钮与面板状态一致 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void CustomPane_VisibleChanged(object sender, System.EventArgs e) { int TempInt = Globals.ThisAddIn.Application.Hwnd; //PanelOnOff.Checked = RightPane.Visible; ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate); ExcelApp.WindowDeactivate -= new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate); //if (!PanelOnOff.Checked) //{ // PanelOnOff.Label = "打开面板"; // PanelOnOff.ScreenTip = "点击打开侧边面板"; // ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate); // ExcelApp.WindowDeactivate -= new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate); //} //else //{ // PanelOnOff.Label = "关闭面板"; // PanelOnOff.ScreenTip = "点击关闭侧边面板"; // ExcelApp.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate); // ExcelApp.WindowDeactivate += new Excel.AppEvents_WindowDeactivateEventHandler(CustomPane_WindowDeactivate); //} } /// <summary> /// 窗体激活事件 /// </summary> /// <param name="WBK"></param> /// <param name="WD"></param> private void CustomPane_WindowActivate(Excel.Workbook WBK, Excel.Window WD) { ExcelApp.WindowActivate -= new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate); ExcelApp = Globals.ThisAddIn.Application; string WBKName = WBK.Name; int TempHwnd = WD.Hwnd; RefreshRightPane(TempHwnd); //设置面板可见性 RightPane.Visible = true; } /// <summary> /// 窗体取消激活事件 /// </summary> /// <param name="WBK"></param> /// <param name="WD"></param> private void CustomPane_WindowDeactivate(Excel.Workbook WBK, Excel.Window WD) { int TempHwnd = WD.Hwnd; if (HwndPaneDic.ContainsKey(TempHwnd)) { HwndPaneDic[TempHwnd].Visible = false; ExcelApp.WindowActivate += new Excel.AppEvents_WindowActivateEventHandler(CustomPane_WindowActivate); } }
更新移动到语雀文档 使用任务窗格 · 语雀 (yuque.com)