用于修改XML数据的Excel宏
我希望生成一个excel宏,并希望得到一些指导。我试图做的是从5个XML文件中获取数据,并将它们放入一个新的工作簿中,然后以线图格式将生成的数据绘制成图形。XML文件中的数据在所有5个文件中都相同,格式如下所示:用于修改XML数据的Excel宏,xml,vba,excel,Xml,Vba,Excel,我希望生成一个excel宏,并希望得到一些指导。我试图做的是从5个XML文件中获取数据,并将它们放入一个新的工作簿中,然后以线图格式将生成的数据绘制成图形。XML文件中的数据在所有5个文件中都相同,格式如下所示: <Sample Secs="25313"> <Pout>215280</Pout> </Sample> <Sample Secs="25562"> <Pout>233627</Pout>
<Sample Secs="25313">
<Pout>215280</Pout>
</Sample>
<Sample Secs="25562">
<Pout>233627</Pout>
</Sample>
标题分别为“Secs”和“Pout”。“Secs”列在A列中的所有5个文件中是通用的,然后唯一的数据在B列中的“Pout”列中。B列的每个文件中大约有1500个数据点。我想完成的是打开所有5个XML文件,从每个文件中提取唯一的数据,然后将数据绘制成图表。在绘制图表之前,我希望新Excel工作簿的格式如下所示:
Secs Pout1 Pout2 Pout3 Pout4 Pout5
“Secs”的数据来自5个文件中的任意一个(因为这是常见的),而B列数据则放在相应的Pout#列中
是否有办法让Excel VBA执行此操作,以及我将如何进行设置?有关详细信息,请参阅
Option Explicit
Private Sub LoadXmlData()
Dim xmlDoc As New DOMDocument60
Dim sSecs As String, sPout As String
Dim iNodes As Integer, i As Integer, t As Integer
Dim sPath1 As String, sPath2 As String, sPath3 As String, _
sPath4 As String, sPath5 As String, sTempPath As String
Range("A1").Value2 = "Secs"
'Set the file paths of your xml documents here
sPath1 = ""
sPath2 = ""
sPath3 = ""
sPath4 = ""
sPath5 = ""
For t = 0 To 4
Select Case t
Case 0
sTempPath = sPath1
Case 1
sTempPath = sPath2
Case 2
sTempPath = sPath3
Case 3
sTempPath = sPath4
Case 4
sTempPath = sPath5
End Select
xmlDoc.Load (sTempPath)
iNodes = xmlDoc.DocumentElement.ChildNodes.Length
'Fill in the first column with the values from the attribute Secs
'Only do it once because the values are the same in each file
If t = 0 Then
For i = 0 To iNodes - 1
Range("A" & i + 2).Value2 = xmlDoc.DocumentElement.ChildNodes.Item(i).Attributes.Item(0).Text
Next i
End If
'Fill in the Pout column
Cells(1, t + 1).Value2 = "Pout" & t + 1
For i = 0 To iNodes - 1
Cells(i + 2, t + 1).Value2 = xmlDoc.DocumentElement.ChildNodes.Item(i).nodeTypedValue
Next i
Next t
End Sub
到目前为止你做了什么编码?
Option Explicit
Private Sub LoadXmlData()
Dim xmlDoc As New DOMDocument60
Dim sSecs As String, sPout As String
Dim iNodes As Integer, i As Integer, t As Integer
Dim sPath1 As String, sPath2 As String, sPath3 As String, _
sPath4 As String, sPath5 As String, sTempPath As String
Range("A1").Value2 = "Secs"
'Set the file paths of your xml documents here
sPath1 = ""
sPath2 = ""
sPath3 = ""
sPath4 = ""
sPath5 = ""
For t = 0 To 4
Select Case t
Case 0
sTempPath = sPath1
Case 1
sTempPath = sPath2
Case 2
sTempPath = sPath3
Case 3
sTempPath = sPath4
Case 4
sTempPath = sPath5
End Select
xmlDoc.Load (sTempPath)
iNodes = xmlDoc.DocumentElement.ChildNodes.Length
'Fill in the first column with the values from the attribute Secs
'Only do it once because the values are the same in each file
If t = 0 Then
For i = 0 To iNodes - 1
Range("A" & i + 2).Value2 = xmlDoc.DocumentElement.ChildNodes.Item(i).Attributes.Item(0).Text
Next i
End If
'Fill in the Pout column
Cells(1, t + 1).Value2 = "Pout" & t + 1
For i = 0 To iNodes - 1
Cells(i + 2, t + 1).Value2 = xmlDoc.DocumentElement.ChildNodes.Item(i).nodeTypedValue
Next i
Next t
End Sub