首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> 關於.NET

Excel 選單系統全攻略:從 Ribbon 到傳統選單的跨版本相容實作

     編輯:關於.NET

Excel 增益集的使用者介面入口在於選單系統。Excel 2007 推出 Ribbon 介面後,傳統選單(Excel 2003 及之前)與 Ribbon 並存多年。企業級增益集若需支援舊版 Excel,必須在同一專案中實現雙模式選單。本文完整示範:Visual Studio 設計工具建立 Ribbon、匯出 Ribbon XML 手動載入、Shared Add-in 建立傳統選單與工具列,最後整合為版本自適應方案。

Ribbon 視覺化設計(VSTO 專案)

  1. 建立 Excel 2013/2016 VSTO 增益集 專案。
  2. 加入新項目 → Ribbon (視覺化設計工具),命名 YYMenu.cs。
  3. 工具箱拖放控制項:Tab → Group → Menu/SplitButton → Button/Separator。

關鍵控制項屬性

控制項關鍵屬性說明
TabControlIdTypeCustom 顯示獨立索引標籤;Office 嵌入內建索引標籤
TabPositionType配合 BeforeOfficeId/AfterOfficeId 排序
GroupLabel群組標題
ButtonControlSizeRibbonControlSizeLarge 顯示大圖示
ButtonImage/OfficeImageId自訂圖示或內建圖示

Ribbon 設計工具介面

Ribbon XML:手動載入與圖示處理

視覺化設計器產生的類別僅適用於 VSTO。Shared Add-in 需手動載入 XML。步驟:

  1. 右鍵設計器 → 匯出功能區至 XML,產生 Ribbon.xml 與 Ribbon.cs。
  2. 將 Ribbon.xml 設為 內嵌資源。
  3. 在 ThisAddIn.cs 覆寫 CreateRibbonExtensibilityObject:
protected override IRibbonExtensibility CreateRibbonExtensibilityObject() {
  return new Ribbon(); // 自動產生的 Ribbon 類別實作 IRibbonExtensibility
}

圖示載入回呼

XML 中 <customUI loadImage="LoadImage">,按鈕設定 image="IconName.png"。實作 LoadImage:

public Bitmap LoadImage(string imageName) {
  var asm = Assembly.GetExecutingAssembly();
  var stream = asm.GetManifestResourceStream("MyAddIn.Resources." + imageName);
  return new Bitmap(stream);
}

圖示檔案需設為 內嵌資源。

傳統選單與工具列(Shared Add-in,Excel 2003 相容)

  1. 建立 Shared Add-in 專案,選擇 C#、目標應用程式勾選 Excel。
  2. 實作 IDTExtensibility2 介面,於 OnConnection 初始化選單。

動態建立選單項目

private CommandBarPopup YYMenu;
private const string MenuTag = "YYMenuTag";

public void AddMenus() {
  CommandBar menuBar = (CommandBar)application.CommandBars.ActiveMenuBar;
  // 移除舊選單避免重複
  try { menuBar.FindControl(MsoControlType.msoControlPopup, Type.Missing, MenuTag, true, true)?.Delete(Type.Missing); } catch { }
  
  YYMenu = (CommandBarPopup)menuBar.Controls.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, menuBar.Controls.Count + 1, true);
  YYMenu.Tag = MenuTag;
  YYMenu.Caption = "歪歪插件";
  YYMenu.BeginGroup = true;

  BuildFinancialGroup(); // 遞迴建立子選單與按鈕
  // ... 其他群組
}

private CommandBarPopup AddPopupButton(CommandBarControls parent, string caption) {
  CommandBarPopup popup = (CommandBarPopup)parent.Add(MsoControlType.msoControlPopup, Type.Missing, Type.Missing, Type.Missing, true);
  popup.Caption = caption;
  return popup;
}

private CommandBarButton AddCommandButton(CommandBarControls parent, string caption, Image icon) {
  CommandBarButton btn = (CommandBarButton)parent.Add(MsoControlType.msoControlButton, Type.Missing, Type.Missing, Type.Missing, true);
  btn.Style = MsoButtonStyle.msoButtonIconAndCaption;
  btn.Caption = caption;
  btn.Tag = caption + "|" + DateTime.Now.Ticks; // 唯一 Tag 避免事件衝突
  btn.Picture = ImageConverterHelper.ImageToPictureDisp(icon); // 需轉 IPictureDisp
  btn.Click += Btn_Click;
  return btn;
}

工具列建立

public void AddToolBars() {
  CommandBar tb;
  try { tb = application.CommandBars["YYToolBar"]; } catch { tb = null; }
  if (tb == null) tb = application.CommandBars.Add("YYToolBar", MsoBarPosition.msoBarTop, false, true);
  // 加入按鈕邏輯同選單,但物件不可共用
  tb.Visible = true;
}

Excel 2003 傳統選單與工具列效果

跨版本整合:單一 Shared Add-in 支援雙模式

  1. 將 VSTO 專案匯出的 Ribbon.xml、Ribbon.cs 複製到 Shared Add-in 專案。
  2. Ribbon.xml 設為內嵌資源,Ribbon.cs 命名空間對齊,類別改為 partial class Connect : IRibbonExtensibility。
  3. 實作 GetCustomUI 回傳 XML 字串,LoadImage、按鈕回呼同前。
  4. OnConnection 依 application.Version 判斷:11.0 (Excel 2003) 建立傳統選單;>=12.0 由 Ribbon XML 自動載入。
public string GetCustomUI(string ribbonID) {
  return GetResourceText("MyAddIn.RibbonMenu.Ribbon.xml");
}

private static string GetResourceText(string name) {
  var asm = Assembly.GetExecutingAssembly();
  foreach (var res in asm.GetManifestResourceNames())
    if (string.Equals(res, name, StringComparison.OrdinalIgnoreCase))
      using (var sr = new StreamReader(asm.GetManifestResourceStream(res)))
        return sr.ReadToEnd();
  return null;
}

Excel 2010 Ribbon 模式

Excel 2003 傳統模式

結語

掌握 Ribbon 視覺化設計、Ribbon XML 手動載入、傳統 CommandBar 動態建立三項技能,並透過版本判斷整合於 Shared Add-in,即可打造支援 Excel 2003 至最新版本的企業級增益集。選單架構就緒後,下一步即是深入 Excel 物件模型實作核心業務邏輯。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved