将数据从XML文件导入到excel

问题描述 投票:0回答:3

我正在尝试将XML文件导入excel,并尝试使用以下代码

Sub Convert_XML_To_Excel_Through_VBA()
'Code from Officetricks.com
'Add referece from Menu: "Tools -> References -> Microsoft XML Vn.0"
Dim iRow As Integer, iCol As Integer
Dim xmlDoc As MSXML2.DOMDocument60, xmlRoot As MSXML2.IXMLDOMNode
Dim xmlNodes As MSXML2.IXMLDOMNode, xmlData As MSXML2.IXMLDOMNode
Set xmlDoc = New MSXML2.DOMDocument60

'Load & Wait till complete XML Data is loaded
xmlDoc.async = False
xmlDoc.validateOnParse = False
xmlDoc.Load (ThisWorkbook.Path & "\Sample.xml")

'XML Loaded. Now Read Elements One by One into XML DOM Objects
Set xmlRoot = xmlDoc.DocumentElement
Set xmlNodes = xmlRoot.FirstChild

'Read XML Data and Load into Excel Sheet by each Node and Chile Node
iRow = 0
For Each xmlNodes In xmlRoot.ChildNodes
    iRow = iRow + 1
    iCol = 0

    For Each xmlData In xmlNodes.ChildNodes
        iCol = iCol + 1
        If xmlData.BaseName = "sheetDataSet" Then
            ThisWorkbook.ActiveSheet.Cells(1, iCol) = xmlData.BaseName
            Dim e

            For Each e In xmlData.ChildNodes
            Debug.Print e.Text
            'ThisWorkbook.ActiveSheet.Cells(iRow, iCol) = xmlData.Text
            Next e
        End If
    Next xmlData
Next xmlNodes
End Sub

[我所能得到的只是一个流文本,例如Header1Header2Yasser10Ahmed20Reda30这是XML内容

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<externalLink
	xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
	xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14"
	xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main">
	<externalBook
		xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" r:id="rId1">
		<sheetNames>
			<sheetName val="Sheet1"/>
		</sheetNames>
		<sheetDataSet>
			<sheetData sheetId="0" refreshError="1">
				<row r="1">
					<cell r="A1" t="str">
						<v>Header1</v>
					</cell>
					<cell r="B1" t="str">
						<v>Header2</v>
					</cell>
				</row>
				<row r="2">
					<cell r="A2" t="str">
						<v>Yasser</v>
					</cell>
					<cell r="B2">
						<v>10</v>
					</cell>
				</row>
				<row r="3">
					<cell r="A3" t="str">
						<v>Ahmed</v>
					</cell>
					<cell r="B3">
						<v>20</v>
					</cell>
				</row>
				<row r="4">
					<cell r="A4" t="str">
						<v>Reda</v>
					</cell>
					<cell r="B4">
						<v>30</v>
					</cell>
				</row>
			</sheetData>
		</sheetDataSet>
	</externalBook>
</externalLink>

如何将数据正确导入工作表?

excel vba xml-parsing
3个回答
2
投票
数据/获取外部数据/从其他来源/从XML数据导入

1
投票
Dim e, r, c, addr As String For Each e In xmlData.ChildNodes For Each r In e.ChildNodes For Each c In r.ChildNodes addr = c.Attributes(0).Value ThisWorkbook.ActiveSheet.Range(addr) = c.Text Next Next Next e

0
投票
Sub LoadXML() Dim xml As Object Set xml = CreateObject("MSXML2.DOMDocument") xml.Load ("c:\temp\excel.xml") xml.setProperty "SelectionNamespaces", "xmlns:ns='http://schemas.openxmlformats.org/spreadsheetml/2006/main'" Dim col As New Collection Dim ndRows, ndCols As Object Set ndRows = xml.SelectNodes("//ns:row") For i = 0 To ndRows.Length - 1 Set ndCols = ndRows(i).SelectNodes("ns:cell/ns:v") For j = 0 To ndCols.Length - 1 Cells(i + 1, j + 1) = ndCols(j).Text Next j Next i End Sub
© www.soinside.com 2019 - 2024. All rights reserved.