先让我们来看一下xml文件的设定:
<?xml version="1.0" encoding="UTF-8"?> <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load"> <ribbon> <tabs> <tab id="TabCustom" label="ExcelAddIn12Tab"> <group id="GroupExcelAddIn12" label="ExcelAddIn12"> <comboBox id="ComboBoxSample" getItemCount ="ComboBoxGetItemCount" getItemLabel ="ComboBoxGetItemLabel" getItemID="ComboBoxGetItemID" getItemScreentip="ComboBoxGetItemScreenTip"/> <dropDown id="DropDownSample" getItemCount="DropDownGetItemCount" getItemLabel ="DropDownGetItemLabel" getItemID ="DropDownGetItemID" getItemScreentip ="DropDowGetItemScreenTip"/> </group> </tab> </tabs> </ribbon> </customUI>
另外getItemID内置方法用处不大,致少在UI显示上我们不能看到它起作用(Debug时你会发现此方法会先于getItemLabel被调用(调用顺序getItemCount->getItemID->getItemLabel->getItemScreenTip)。
下面是Ribbon1.cs
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using Office = Microsoft.Office.Core; // TODO: Follow these steps to enable the Ribbon (XML) item: // 1: Copy the following code block into the ThisAddin, ThisWorkbook, or // ThisDocument class. // protected override Microsoft.Office.Core.IRibbonExtensibility //CreateRibbonExtensibilityObject() // { // return new Ribbon1(); // } // 2. Create callback methods in the "Ribbon Callbacks" region of this class to // handle user actions, such as clicking a button. Note: if you have exported // this Ribbon from the Ribbon designer, move your code from the event handlers // to the callback methods and modify the code to work with the Ribbon // extensibility (RibbonX) programming model. // 3. Assign attributes to the control tags in the Ribbon XML file to identify // the appropriate callback methods in your code. // For more information, see the Ribbon XML documentation in the Visual Studio // Tools for Office Help. namespace ExcelAddIn12 { [ComVisible(true)] public class Ribbon1 : Office.IRibbonExtensibility { private Office.IRibbonUI ribbon; public Ribbon1() { } #region IRibbonExtensibility Members public string GetCustomUI(string ribbonID) { return GetResourceText("ExcelAddIn12.Ribbon1.xml"); } #endregion #region Ribbon Callbacks //Create callback methods here. For more information about adding callback methods, select the Ribbon XML item in Solution Explorer and then press F1 public void Ribbon_Load(Office.IRibbonUI ribbonUI) { this.ribbon = ribbonUI; } #endregion #region Helpers private static string GetResourceText(string resourceName) { Assembly asm = Assembly.GetExecutingAssembly(); string[] resourceNames = asm.GetManifestResourceNames(); for (int i = 0; i < resourceNames.Length; ++i) { if (string.Compare(resourceName, resourceNames[i], StringComparison.OrdinalIgnoreCase) == 0) { using (StreamReader resourceReader = new StreamReader(asm.GetManifestResourceStream(resourceNames[i]))) { if (resourceReader != null) { return resourceReader.ReadToEnd(); } } } } return null; } #endregion #region MyCode public int ComboBoxGetItemCount(Office.IRibbonControl Control) { return 5; } public string ComboBoxGetItemLabel(Office.IRibbonControl Control, int Index) { return "ComboBoxItem" + Index.ToString(); } public string ComboBoxGetItemID(Office.IRibbonControl Control, int Index) { return "cb" + Index.ToString(); } public int DropDownGetItemCount(Office.IRibbonControl Control) { return 3; } public string DropDownGetItemLabel(Office.IRibbonControl Control, int Index) { return "DropDownItem" + Index.ToString(); } public string DropDownGetItemID(Office.IRibbonControl Control, int Index) { return "dd" + Index.ToString(); } public string ComboBoxGetItemScreenTip(Office.IRibbonControl Control, int Index) { return string.Format("This is \"{0}\", its id is {1}", "ComboBoxItem" + Index.ToString(), "cb" + Index.ToString()); } public string DropDowGetItemScreenTip(Office.IRibbonControl Control, int Index) { return string.Format("This is \"{0}\", its id is {1}", "DropDownItem" + Index.ToString(), "dd" + Index.ToString()); } #endregion } }