【问题标题】:Populate CustomUI for Excel 365 Backstage为 Excel 365 后台填充 CustomUI
【发布时间】:2016-07-18 12:45:59
【问题描述】:

我正在为自己的目的开发一个版本的 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 上发布了这个问题;在这里重复以获得更广泛的受众。 :-)

【问题讨论】:

  • 您应该包括您谈论的 XML sn-ps,以及您收到的确切错误消息。

标签: xml vb.net office365


【解决方案1】:

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 在新版本命名空间下插入 Ribbon 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();
            }
        }
    }

【讨论】:

  • 谢谢!我更改了 XML 以反映新的命名空间,但我没有注意到更改库版本。非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多