|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码:
- Sub extractXml()
- Dim xdoc As Object, arr()
- Set xdoc = CreateObject("MSXML2.DOMDocument")
- '前期绑定
- 'Dim xdoc As New DOMDocument60 '声明的同时创建XML对象
- Dim b As Boolean, root
- b = xdoc.Load(ThisWorkbook.Path & "\数据源.xml")
- If b = True Then
- Set root = xdoc.DocumentElement '获取根节点
- Dim i As Integer, j As Integer
- ReDim arr(1 To root.ChildNodes(0).ChildNodes.Length + 1, 1 To root.ChildNodes(0).ChildNodes(0).Attributes.Length)
- '获取列标题
- With root.ChildNodes(0).ChildNodes(0) '根节点的子节点
- For i = 0 To .Attributes.Length - 1
- arr(1, i + 1) = .Attributes(i).nodeName
- Next i
- End With
- '获取行数据
- For i = 0 To root.ChildNodes(0).ChildNodes.Length - 1
- With root.ChildNodes(0).ChildNodes(i)
- For j = 0 To .Attributes.Length - 1
- arr(i + 2, j + 1) = .Attributes(j).Text
- Next j
- End With
- Next i
- With Worksheets("导入XML数据")
- .Cells.Clear
- Union(.Columns("a:a"), .Columns("c:c"), .Columns("p:p")).NumberFormatLocal = "@"
- .Range("a1").Resize(UBound(arr), UBound(arr, 2)) = arr
- End With
- Else
- MsgBox "加载失败,请确认同级目录是否有待读取xml文件"
- End If
- End Sub
复制代码 附件:
|
|