| "NorthwindCustomerOrders").Name _
= "NorthwindCustomerOrders_Map"
代码得到这个新建的XML映射的引用,将它保存为映射变量,然后添加一个ListObject到当前活动工作表。你所选择的列表面板列,本示例中是列A到J,包括标题行下面的许多行都是用来容纳数据的。
''''Specify the cells where the mapped data should go upon import
Dim map As Excel.XmlMap = _
xl.ActiveWorkbook.XmlMaps("NorthwindCustomerOrders_Map")
xl.Range("A1", "J1").Select()
Dim list As Excel.ListObject = _
CType(xl.ActiveSheet, Excel.Worksheet).ListObjects.Add
下一步是映射XML数据的特定元素到列表的每个列。SetValue方法持有所使用的映射的引用,并使用一个XPath表达式来指示哪些元素保存在哪一列。下面的代码设置列A容纳CustomerID元素,然后设置该列标题为“Customer ID”。你可以设置以相同方式设置其他列的内容和标题。
list.ListColumns(1).XPath.SetValue(map, _
"/NorthwindCustomerOrders/Customers/CustomerID")
xl.Range("A1").Value = "Customer ID"
现在要设置ListObject的结构,因为下一步将导入XML数据。Import方法读取磁盘文件,并且这里使用ImportXml方法直接从DataSet 的GetXml方法中读取XML,并未将XML数据保存到磁盘上。该过程在最后打开“XML源”任务面板,方便你查看。
''''Import the XML data
xl.ActiveWorkbook.XmlMaps("NorthwindCustomerOrders_Map"). _
ImportXml(ds.GetXml)
''''Open the XML Source Task Pane
xl.DisplayXMLSourcePane(map)
Catch ex As Exception
End Try
End Sub
上一页 [1] [2] |