【问题标题】:Retrieve CustomXMLParts in Excel Addin using VB.NET使用 VB.NET 在 Excel 插件中检索 CustomXMLParts
【发布时间】:2014-05-23 19:44:46
【问题描述】:

我需要一些帮助来检索存储在使用 VB.NET 的 Excel 插件中的 CustomXMLPart 中的值。我已经搜索过,并没有找到很多关于这个的细节。根据我的发现,我拥有的代码应该可以工作。起初,我认为我的 xml 部分没有被添加以便在会话中持续存在,但我在 (3) 和之后 (4) 添加我的自定义 xml 部分之前显示了 CustomXMLParts 集合的计数,并且计数确实增加了一个。我在打开保存的 Excel 工作簿时也显示了计数,并且在将我的 CustomXMLPart 添加到集合后它是相同的数字 (4)。这是下面的相关代码。任何帮助将不胜感激。需要更多信息,请告诉我。

在 Excel 插件中,我有一个弹出窗口,我要求用户输入,这就是我需要保留的信息。在后面的代码中,我创建了 xml 并添加到集合中。

代码:

    Dim workbook As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
    Dim xml As String

    xml = "<?xml version=""1.0"" encoding=""utf-8"" ?>" _
        & "<refreshViewPointData xmlns=""http://refreshviewpointdata.com"">" _
        & "<dataReference>" _
        & "<system>" & cboSystem.Text & "</system>" _
        & "<library>" & cboLibraries.Text & "</library>" _
        & "<view>" & txtObject.Text & "</view>" _
        & "<headers>" & chkInclColumnHdrs.Checked.ToString() & "</headers>" _
        & "<numOfRecords>" & txtRowCount.Text & "</numOfRecords>" _
        & "<reference>" & txtReference.Text & "</reference>" _
        & "</dataReference>" _
        & "</refreshViewPointData>"

    workbook.CustomXMLParts.Add(xml, System.Type.Missing)

在 ThisAddIn.vb 文件的 ThisAddIn_Startup() 方法中,我尝试检索 CustomXMLPart。我从 ThisAddIn_Startup() 调用 RetrieveCustomXMLPart() 方法。

RetrieveCustomXMLParts() 的代码:

    Dim parts As Microsoft.Office.Core.CustomXMLParts

    parts = Application.ActiveWorkbook.CustomXMLParts.SelectByNamespace("http://refreshviewpointdata.com")

    If parts.Count > 0 Then
        RefreshData(parts.ToString())
    End If

RefreshData() 的代码:

    Dim r As New RibbonViewPoint
    Dim viewXMLPart As New XmlDocument

    Dim system, library, sObject, reference As String
    Dim headers As Boolean
    Dim numRecords As Integer

    'Load the xml from the string.
    viewXMLPart.LoadXml(part)

    'Retrieve the values from the xml document.
    system = viewXMLPart.SelectSingleNode("/dataReference/system").Value
    library = viewXMLPart.SelectSingleNode("/dataReference/library").Value
    sObject = viewXMLPart.SelectSingleNode("/dataReference/view").Value
    headers = CType(viewXMLPart.SelectSingleNode("/dataReference/headers").Value, Boolean)
    numRecords = CType(viewXMLPart.SelectSingleNode("/dataReference/numOfRecords").Value, Integer)
    reference = viewXMLPart.SelectSingleNode("/dataReference/reference").Value

    'Call method to run the object to refresh the data.
    r.RunSelectedObject(system, library, sObject, headers, reference, numRecords)

在 RetrieveCustomXMLPart() 方法中,SelectByNamespace() 方法没有返回我的 CustomXMLPart,它显然与我传入的名称空间相同。有人知道出了什么问题吗?

另外,如果有人对我不明白的其他事情有任何见解,那也很好。在 RefreshData() 方法中,我将 viewXMLPart 变量作为 XMLDocument 来加载数据并从那里获取值。在我将它定义为“Dim viewXMLPart As New Microsoft.Office.Core.CustomXMLPart”之前,它一直给我一个语法错误,说“'Microsoft.Office.Core.CustomXMLPartClass.Friend Sub New()' 在这个上下文,因为它是朋友。”

谢谢!!!

【问题讨论】:

    标签: vb.net excel visual-studio-2012 add-in


    【解决方案1】:

    找到了解决办法。以下是有效的。

        Dim workbook = Application.ActiveWorkbook
        Dim customXMLParts = workbook.CustomXMLParts.SelectByNamespace("urn:viewpoint-refresh")
        Dim customXMLPart = customXMLParts.Cast(Of CustomXMLPart)().FirstOrDefault()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      相关资源
      最近更新 更多