函数类似于Excel中的importxml?

我喜欢使用谷歌文档function = importxml(),但很想知道在Excel 2010中是否有类似的东西? 我似乎无法find程序自动从链接的XML文件中提取数据的方法。

例如,我希望能够设置一个标题为“Item Name”的列,然后让下一列将前一列中用户input的项目名称附加到此URL

http://util.eveuniversity.org/xml/itemLookup.php?name= 

然后parsing生成的XML文件以返回typesID。 这是在谷歌文档使用完成

 =importxml(concatenate("http://util.eveuniversity.org/xml/itemLookup.php?name=",A3);"//itemLookup/typeID") 

A3是具有项目名称的列,在这种情况下将是Tritanium,并将数据导出到生成的XML文件中

 http://util.eveuniversity.org/xml/itemLookup.php?name=Tritanium 

它返回值34。

我有一个约20个项目名称的列表,谷歌文档每次打开文件时自动更新项目ID。 有什么办法让Excel 2010复制这个function?

谢谢!

你将需要编写你自己的UDF。

一种方法是使用MSXML2库,如下所示:

 Function GetData(sName As String, sItem As String, Optional sURL = "") As Variant Dim oHttp As New MSXML2.XMLHTTP60 Dim xmlResp As MSXML2.DOMDocument60 Dim result As Variant On Error GoTo EH If sURL = "" Then sURL = "http://util.eveuniversity.org/xml/itemLookup.php?name=" End If 'open the request and send it oHttp.Open "GET", sURL & sName, False oHttp.Send 'get the response as xml Set xmlResp = oHttp.responseXML ' get Item GetData = xmlResp.getElementsByTagName(sItem).Item(0).Text ' Examine output of these in the Immediate window Debug.Print sName Debug.Print xmlResp.XML CleanUp: On Error Resume Next Set xmlResp = Nothing Set oHttp = Nothing Exit Function EH: GetData = CVErr(xlErrValue) GoTo CleanUp End Function 

像这样调用它(其中A5包含所需的typeName

 =GetData(A5, "typeID") 

问题是从2013年,一段时间过去了…

使用Excel 2013,有一个functionWEBSERVICE来加载XML文档,这将做你想要的。

还有FILTERXML使用XPathsearch加载的XML文档。

 Function ImportXML(url As String, query As String) Dim document As MSXML2.DOMDocument60 Dim http As New MSXML2.XMLHTTP60 http.Open "GET", url, False http.send Set document = http.responseXML ImportXML = document.SelectSingleNode(query).nodeTypedValue End Function 

数据菜单上的“从networking”function将在线数据直接导入电子表格。 XML数据导入也可从“其他源”子菜单下find,也在数据菜单中列出。

创build的连接通过“数据”菜单上的“连接”对话框进行pipe理。

使用loggingmacros创build“From Web”连接时的示例代码:

 Sub Macro1() ' Macro1 Macro With ActiveSheet.QueryTables.Add(Connection:= _ "URL;http://en.wikipedia.org/wiki/Microsoft_Excel" _ , Destination:=Range("$A$1")) .Name = _ "?affID=110195&tt=270912_7a_3912_6&babsrc=HP_ss&mntrId=3e2fc48700000000000088532eb428ec" .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .BackgroundQuery = True .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .WebSelectionType = xlEntirePage .WebFormatting = xlWebFormattingNone .WebPreFormattedTextToColumns = True .WebConsecutiveDelimitersAsOne = True .WebSingleBlockTextImport = False .WebDisableDateRecognition = False .WebDisableRedirections = False .Refresh BackgroundQuery:=False End With End Sub