为Excel 365 Backstage填充CustomUI

时间:2016-03-30 13:20:28

标签: xml vb.net office365

我正在为自己的目的开发一个版本的CustomUI实用程序。我从MSDN的代码开始,它为Office 2007设置了原始的CustomUI元素,但我找不到如何在这个版本中实现后台。

原始code taken from MSDN是:

Using document As SpreadsheetDocument = SpreadsheetDocument.Open(fileName, True) 
  ' You can only have a single ribbon extensibility part. 
  ' If the part doesn't exist, add it. 
  Dim part = document.RibbonExtensibilityPart 
  If part Is Nothing Then 
    part = document.AddRibbonExtensibilityPart 
  End If 
  part.CustomUI = New CustomUI(customUIContent) 
  part.CustomUI.Save() 
End Using 

如果customUIcontent包含带有<backstage>标记的XML(使用xmlns= "http://schemas.microsoft.com/office/2009/07/customui"),则此代码将失败。

我使用原始的CustomUI实用程序将示例后台插入工作簿。在检查XML(在手表中)时,我找到了一个额外的元素RibbonAndBackstageCustomizationsPart。我试图复制原始代码,用RibbonAndBackstageCustomizationsPart代替RibbonExtensibilityPart - 但是这个代码失败了 part.CustomUI = New CustomUI(customUIContent)行。

在进一步检查我的示例中的XML内容时,我发现innerXML和outerXML具有原始XML的变体,其中标记具有"mso14"前缀(这可能是为什么CustomUI显示CustomUI14.xml与CustomUI12分开的原因)。 xml部分)。

我已经广泛搜索了有关如何使用我的XML填充RibbonAndBackstageCustomizationsPart元素的帮助,但未能找到正确的语法。

有人能告诉我填充RibbonAndBackstageCustomizationsPart元素的正确方法吗?

我在VS 2012中使用Open XML 2.5 SDK。

非常感谢。

P.S。:我今天早些时候在ericwhite.com上发布了这个问题;在这里重复以获得更广泛的受众: - )

1 个答案:

答案 0 :(得分:0)

Office有两种版本的自定义UI。第一个版本位于名称空间http://schemas.microsoft.com/office/2006/01/customui下,第二个版本位于新名称空间http://schemas.microsoft.com/office/2009/07/customui下。

后台视图是从Office 2010引入的,我们需要使用新的名称空间。在Open XML 2.5中,我们应该使用RibbonAndBackstageCustomizationsPart在新版本名称空间下插入功能区XML。

以下示例使用Open XML 2.5在Office后台视图中添加选项卡供您参考:

    public void Main()
    {

        string docName = @"C:\book1.xlsx";
        string customUIContent = "<customUI xmlns=\"http://schemas.microsoft.com/office/2009/07/customui\">"
                               + "<backstage>"
                               + "<tab id = \"customTab1\" label = \"customTab1\" ></tab>"
                               + "</backstage>"
                               + "</customUI >";
        using (SpreadsheetDocument document = SpreadsheetDocument.Open(docName, true))
        {
            if (document.RibbonAndBackstageCustomizationsPart == null)
            {
                document.AddRibbonAndBackstageCustomizationsPart();
                document.RibbonAndBackstageCustomizationsPart.CustomUI = new DocumentFormat.OpenXml.Office2010.CustomUI.CustomUI(customUIContent);
                document.RibbonAndBackstageCustomizationsPart.CustomUI.Save();
            }
        }
    }