|
楼主 |
发表于 2013-6-18 19:29
|
显示全部楼层
[ComVisible(true)]
public class RibbonUI2007 : ExcelRibbon
{public override string GetCustomUI(string uiName)
{
string ribbonxml = string.Empty;
//读取CustomUI.xml文件,定制RibbonX。
try
{
ribbonxml = ResourceHelper.GetResourceText("CustomUI.xml"); //CustomUI.xml必须是UTF-8格式
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return ribbonxml;
}
}
代码用的是GetCustomUI重载,DNA的原函数如下:
public virtual string GetCustomUI(string RibbonID)
{
if (RibbonID != "Microsoft.Excel.Workbook")
{
Debug.Print("ExcelRibbon.GetCustomUI - Invalid RibbonID for Excel. RibbonID: {0}", RibbonID);
return null;
}
// Default behaviour for GetCustomUI is to look in DnaLibrary.
// We return a CustomUI based on the version of Excel, as follows:
// If Excel12, look for a CustomUI with namespace... If not found, return nothing
// If Excel14 or bigger look for CustomUI with namespace ... If not found, look for ... else return nothing.
// (not sure how to future-proof...)
Dictionary<string, string> customUIs = new Dictionary<string, string>();
foreach (XmlNode customUI in this.DnaLibrary.CustomUIs)
{
customUIs[customUI.NamespaceURI] = customUI.OuterXml;
}
if (ExcelDnaUtil.ExcelVersion >= 14.0)
{
if (customUIs.ContainsKey(NamespaceCustomUI2010))
{
return customUIs[NamespaceCustomUI2010];
}
if (customUIs.ContainsKey(NamespaceCustomUI2007))
{
return customUIs[NamespaceCustomUI2007];
}
return null;
}
if (ExcelDnaUtil.ExcelVersion >= 12.0)
{
if (customUIs.ContainsKey(NamespaceCustomUI2007))
{
return customUIs[NamespaceCustomUI2007];
}
return null;
}
throw new InvalidOperationException("Not expected to provide CustomUI string for Excel version < 12.0");
}
|
|