通过VBA将XML加载到Excel中

Fox*_*x87 9 xml api excel schema vba

我有点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
Run Code Online (Sandbox Code Playgroud)

目前,数据就像这样,非结构化.如何将其自动转换为表格?

<result>
<entry>
<published_date>20130201</published_date>
<post_count>18</post_count>
</entry>
Run Code Online (Sandbox Code Playgroud)

谢谢,

::最终解决方案::

 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
Run Code Online (Sandbox Code Playgroud)

Mad*_*hew 9

"硬编码"方式是这样的:

从这开始

<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>
Run Code Online (Sandbox Code Playgroud)

并且您想获得一个包含两列的Excel:

**published_date** |  **post_count**
20130201       |           18
20120201       |           15
Run Code Online (Sandbox Code Playgroud)

这样我们就可以假设你的XML总是如此

<result><entry><Element>VALUE</Element><Element...n>VALUE</Element...n></entry>
Run Code Online (Sandbox Code Playgroud)

重要说明:在PowerPoint,Excel中打开VBA编辑器.单词并添加对"Microsoft XML,v3.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>
Run Code Online (Sandbox Code Playgroud)

代码阅读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
Run Code Online (Sandbox Code Playgroud)

当然,在你的情况下,你需要调整你的日常工作:

结果 - >提供代码中的EmpDetails
条目 - >提供的代码中的Employee

加上任何其他必要的调整.


通过这种方式,您可以拥有所需的许多"入口"和"入门子"元素.

实际上,循环遍历"条目"中的所有元素,您将获得COLUMN,然后每个新条目都是新的ROW.

不幸的是,我没有优秀的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
Run Code Online (Sandbox Code Playgroud)

这种方式应该是这样的:

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
Run Code Online (Sandbox Code Playgroud)