加载XML到Excel通过VBA(Load XML into Excel through VBA)

2019-09-02 03:59发布

我有位VBA正在加载通过VBA的XML文件。 然而,当它导入它是所有在一列中,而不是分成表。

当我手动导入此通过数据标签我得到警告,没有模式,但也问我是否想Excel将基于源数据创建一个。 然后,这会将所有的数据在一个不错的表。

我想这对我目前的VBA代码内自动发生:

VBA的样子

      Sub refresh()

'--------------------------------1. Profile IDs-----------------------------------'


'date variables

Dim start_period As String
start_period = Sheets("Automated").Cells(1, 6).Value
Dim end_period As String
end_period = Sheets("Automated").Cells(1, 7).Value

'report id variable names
Dim BusinessplanningReportID As String

'--------------------------------REST queries--------------------------------'
Dim Businessplanning As String



'REST query values
Businessplanning = "URL;http://api.trucast.net/2/saved_searches/00000/pivot/content_volume_trend/?apikey=0000000&start=" + start_period + "&end=" + end_period + "&format=xml"




'--------------------------------------------Data connections-----------------------------------'
'key metrics
With Worksheets("Sheet1").QueryTables.Add(Connection:=Businessplanning, Destination:=Worksheets("Sheet1").Range("A1"))

  .RefreshStyle = xlOverwriteCells
  .SaveData = True

End With

目前的数据,然后提出自己这样的,非结构化的。 我怎么能自动转成表呢?

<result>
<entry>
<published_date>20130201</published_date>
<post_count>18</post_count>
</entry>

谢谢,

最终的解决方案:: ::

 Sub XMLfromPPTExample2()
Dim XDoc As MSXML2.DOMDocument
Dim xresult As MSXML2.IXMLDOMNode
Dim xentry As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode
Dim start_period As String
    start_period = Sheets("Automated").Cells(1, 6).Value
    Dim end_period As String
    end_period = Sheets("Automated").Cells(1, 7).Value
Dim wb As Workbook
Dim Col As Integer
Dim Row As Integer


Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load ("http://api.trucast.net/2/saved_searches/0000/pivot/content_volume_trend/?apikey=00000&start=" + start_period + "&end=" + end_period + "&format=xml")
LoadOption = xlXmlLoadImportToList

Set xresult = XDoc.DocumentElement
Set xentry = xresult.FirstChild


Col = 1
Row = 1

For Each xentry In xresult.ChildNodes
 Row = 1


    For Each xChild In xentry.ChildNodes
      Worksheets("Sheet2").Cells(Col, Row).Value = xChild.Text
             'MsgBox xChild.BaseName & " " & xChild.Text
      Row = Row + 1
      'Col = Col + 1

          Next xChild
'Row = Row + 1
Col = Col + 1
Next xentry

End Sub

Answer 1:

“硬编码”的方法是:

以此为出发点

<result>
   <entry>
      <published_date>20130201</published_date>
      <post_count>18</post_count>    
   </entry>
  <entry>
      <published_date>20120201</published_date>
      <post_count>15</post_count>    
   </entry>

你想获得一个excel两列:

**published_date** |  **post_count**
20130201       |           18
20120201       |           15

这样我们就可以假设,在你的XML你将永远有

<result><entry><Element>VALUE</Element><Element...n>VALUE</Element...n></entry>

重要提示:打开在PowerPoint,Excel中..字VBA编辑器并添加为“Microsoft XML,3.0”的引用(该引用是Office 2000 ...你可能有其他人)。

来源: http://vba2vsto.blogspot.it/2008/12/reading-xml-from-vba.html

Employee.XML

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<EmpDetails>
<Employee>
<Name>ABC</Name>
<Dept>IT-Software</Dept>
<Location>New Delhi</Location>
</Employee>
<Employee>
<Name>XYZ</Name>
<Dept>IT-Software</Dept>
<Location>Chennai</Location>
</Employee>
<Employee>
<Name>IJK</Name>
<Dept>HR Operations</Dept>
<Location>Bangalore</Location>
</Employee>
</EmpDetails>

代码来读取上面的XML

Sub XMLfromPPTExample()
Dim XDoc As MSXML2.DOMDocument
Dim xEmpDetails As MSXML2.IXMLDOMNode
Dim xEmployee As MSXML2.IXMLDOMNode
Dim xChild As MSXML2.IXMLDOMNode

Set XDoc = New MSXML2.DOMDocument
XDoc.async = False
XDoc.validateOnParse = False
XDoc.Load ("C:\Emp.xml")
Set xEmpDetails = XDoc.documentElement
Set xEmployee = xEmpDetails.firstChild
For Each xEmployee In xEmpDetails.childNodes
For Each xChild In xEmployee.childNodes
MsgBox xChild.baseName & " " & xChild.Text
Next xChild
Next xEmployee
End Sub

在你的情况,当然,您需要调整您的例行:

结果 - 在代码> EmpDetails提供
条目 - 在代码>员工提供

加上其它必要的调整。


通过这种方式,你可以有尽可能多的“入口”,你想“项子”元素。

事实上,通过你的“入口”内的所有元素循环,你会得到你的专栏,然后每一个新的条目是新一行。

不幸的是,我没有Excel的MAC,所以我只是把逻辑,你应该检查sintax自己的...这样你打造你想要的工作表上一个EXCEL表格。

Dim col = 1; Dim row=1;

For Each xEmployee In xEmpDetails.childNodes
    col = 1
    For Each xChild In xEmployee.childNodes
       Worksheets("NAMEOFTHESHEET").Cells(col, row).Value = xChild.Text
       MsgBox xChild.baseName & " " & xChild.Text
       col = col + 1;
    Next xChild
row = row+1;
Next xEmployee

THE WAY CORRET应该是这样的:

LoadOption:= xlXmlLoadImportToList?

你正在从URL调用的XML,但我强烈建议尝试在开始在磁盘上的XML文件的工作,并检查它是否是正确有效的。 所以,你应该做的就是从这个“WebService的”示例XML,然后将其保存在磁盘上。 一个尝试按以下方式加载它:

   Sub ImportXMLtoList()
    Dim strTargetFile As String
    Dim wb as Workbook

         Application.Screenupdating = False
         Application.DisplayAlerts = False
         strTargetFile = "C:\example.xml"
         Set wb = Workbooks.OpenXML(Filename:=strTargetFile,        LoadOption:=xlXmlLoadImportToList)
         Application.DisplayAlerts = True

         wb.Sheets(1).UsedRange.Copy ThisWorkbook.Sheets("Sheet2").Range("A1")
         wb.Close False
         Application.Screenupdating = True
    End Sub


Answer 2:

我使用的代码,其他部分我已经发现了几节。 下面的代码会提示用户选择所需的XML文件,并允许他们简单地添加/所选文件导入到他们现有的映射,而无需打开一个新的文件。

Sub Import_XML()
'
' Import_XML Macro
'
    'Select the file
    Fname = Application.GetOpenFilename(FileFilter:="xml files (*.xml), *.xml", MultiSelect:=False)

    'Check if file selected
    If Fname = False Then
        Exit Sub
        Else

    End If

    'Import selected XML file into existing, custom mapping

    Range("B5").Select
    ActiveWorkbook.XmlMaps("Result_file_Map").Import URL:=Fname
End Sub


文章来源: Load XML into Excel through VBA